Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a very simple sample code for ASP.NET Azure SignalR support #52

Merged
merged 9 commits into from
Sep 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions aspnet-samples/ChatRoom/ChatHub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;

namespace ChatRoom
{
public class ChatHub : Hub
{
public void Send(string name, string message)
{
// Call the broadcastMessage method to update clients.
Clients.All.broadcastMessage(name, message);
}
}
}
239 changes: 239 additions & 0 deletions aspnet-samples/ChatRoom/ChatRoom.csproj

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions aspnet-samples/ChatRoom/ChatRoom.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27703.2042
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChatRoom", "ChatRoom.csproj", "{1833E2BF-4D58-4288-8CBA-869981888972}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1833E2BF-4D58-4288-8CBA-869981888972}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1833E2BF-4D58-4288-8CBA-869981888972}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1833E2BF-4D58-4288-8CBA-869981888972}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1833E2BF-4D58-4288-8CBA-869981888972}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E4529C3E-C868-49EE-BBEA-67DF61A5DAC9}
EndGlobalSection
EndGlobal
38 changes: 38 additions & 0 deletions aspnet-samples/ChatRoom/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ChatRoom")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("ChatRoom")]
[assembly: AssemblyCopyright("Copyright © Microsoft")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("1833e2bf-4d58-4288-8cba-869981888972")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
79 changes: 79 additions & 0 deletions aspnet-samples/ChatRoom/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Build Your First Azure SignalR Service Application for ASP.NET SignalR

In [ChatRoomLocal sample](../ChatRoomLocal) you have learned how to use SignalR to build a chat room application. In that sample, the SignalR runtime (which manages the client connections and message routing) is running on your local machine. As the number of the clients increases, you'll eventually hit a limit on your machine and you'll need to scale up your machine to handle more clients. This is usually not an easy task. In this tutorial, you'll learn how to use Azure SignalR Service to offload the connection management part to the service so that you don't need to worry about the scaling problem.

## Provision a SignalR Service

First let's provision a SignalR service on Azure.

1. Open Azure portal, click "Create a resource" and find "SignalR Service" in "Web + Mobile".

![signalr-1](../../docs/images/signalr-1.png)

2. Click "Create", and then fill in basic information including resource name, resource group and location.

![signalr-2](../../docs/images/signalr-2.png)

Resource name will also be used as the DNS name of your service endpoint. So you'll get a `<resource_name>.service.signalr.net` that your application can connect to.

Select a pricing tier. There're two pricing tiers:

* Free: which can handle 100 connections at the same time and can send and receive one million messages in a month.
* Basic: which has 1000 concurrent connections and 75 millions message per month limit for *one unit*. You can scale up to 10 units for a single service instance and you'll be charged by the number of units you use.

3. Click "Create", your SignalR service will be created in a few minutes.

![signalr-3](../../docs/images/signalr-3.png)

After your service is ready, go to the **Keys** page of your service instance and you'll get two connection strings that your application can use to connect to the service.

The connection string is in the form of the following:

```
Endpoint=<service_endpoint>;AccessKey=<access_key>;
```

## Update Chat Room to Use Azure SignalR Service

Then, let's update the chat room sample to use the new service you just created.

Let's look at the key changes:

1. In [Startup.cs](Startup.cs), instead of calling `MapSignalR()`, you need to call `MapAzureSignalR({your_applicationName})` and pass in connection string to make the application connect to the service instead of hosting SignalR by itself. Replace `{YourApplicationName}` to the name of your application, this is the unique name to distinguish this application with your other application. You can use `this.GetType().FullName` as the value.

```cs
public void Configuration(IAppBuilder app)
{
// Any connection or hub wire up and configuration should go here
app.MapAzureSignalR(this.GetType().FullName);
}
```

You also need to reference the service SDK before using these APIs. Open the **Tools | Library Package Manager | Package Manager Console** and run command:

```powershell
Install-Package Microsoft.Azure.SignalR.AspNet -Version 1.0.0-preview1-10233
```

> Note
>
> This is a preview version of SDK, the version number is tend to change.

Other than these changes, everything else remains the same, you can still use the hub interface you're already familiar with to write business logic.

> Under the hood, an endpoint `/signalr/negotiate` is exposed for negotiation by Azure SignalR Service SDK. It will return a special negotiation response when clients try to connect and redirect clients to service endpoint from the connection string.

2. Now set the connection string in the web.config file.

```xml
<configuration>
<connectionStrings>
<add name="Azure:SignalR:ConnectionString" connectionString="Endpoint=...;AccessKey=..."/>
</connectionStrings>
...
</configuration>
```

Press **F5** to run the project in debug mode. You can see the application runs as usual, just instead of hosting a SignalR runtime by itself, it connects to the SignalR service running on Azure.

In this sample, you have learned how to use Azure SignalR Service to replace your self-hosted SignalR runtime.
Loading