SignalR Scaleout with Azure Service Bus (SignalR 1.x)

by Patrick Fletcher

Warning

This documentation isn't for the latest version of SignalR. Take a look at ASP.NET Core SignalR.

In this tutorial, you will deploy a SignalR application to a Windows Azure Web Role, using the Service Bus backplane to distribute messages to each role instance.

Diagram that illustrates the relationship between the Service Bus Namespace Topic, Web Roles, and available computers and accounts.

Prerequisites:

The service bus backplane is also compatible with Service Bus for Windows Server, version 1.1. However, it is not compatible with version 1.0 of Service Bus for Windows Server.

Pricing

The Service Bus backplane uses topics to send messages. For the latest pricing information, see Service Bus. At the time of this writing, you can send 1,000,000 messages per month for less than $1. The backplane sends a service bus message for each invocation of a SignalR hub method. There are also some control messages for connections, disconnections, joining or leaving groups, and so forth. In most applications, the majority of the message traffic will be hub method invocations.

Overview

Before we get to the detailed tutorial, here is a quick overview of what you will do.

  1. Use the Windows Azure portal to create a new Service Bus namespace.

  2. Add these NuGet packages to your application:

  3. Create a SignalR application.

  4. Add the following code to Global.asax to configure the backplane:

    protected void Application_Start()
    {
        string connectionString = "Service Bus connection string";
        GlobalHost.DependencyResolver.UseServiceBus(connectionString, "YourAppName");
    
        RouteTable.Routes.MapHubs();
        // ...
    }
    

For each application, pick a different value for "YourAppName". Do not use the same value across multiple applications.

Create the Azure Services

Create a Cloud Service, as described in How to Create and Deploy a Cloud Service. Follow the steps in the section "How to: Create a cloud service using Quick Create". For this tutorial, you do not need to upload a certificate.

Screenshot of the NEW pane with the Cloud Service option and icon being highlighted in the application as well as a red circle.

Create a new Service Bus namespace, as described in How to Use Service Bus Topics/Subscriptions. Follow the steps in the section "Create a Service Namespace".

Screenshot of the Add a new namespace screen with entries entered in the Namespace Name and Region fields.

Note

Make sure to select the same region for the cloud service and the Service Bus namespace.

Create the Visual Studio Project

Start Visual Studio. From the File menu, click New Project.

In the New Project dialog box, expand Visual C#. Under Installed Templates, select Cloud and then select Windows Azure Cloud Service. Keep the default .NET Framework 4.5. Name the application ChatService and click OK.

Screenshot of the New Project screen with the Windows Azure Cloud Service Visual C # option being highlighted.

In the New Windows Azure Cloud Service dialog, select ASP.NET MVC 4 Web Role. Click the right-arrow button (>) to add the role to your solution.

Hover the mouse over the new role, so the pencil icon visible. Click this icon to rename the role. Name the role "SignalRChat" and click OK.

Screenshot of the New Windows Azure Cloud Service screen with the Signal R Chat option highlighted in the Windows Azure Cloud Service solution pane.

In the New ASP.NET MVC 4 Project wizard, select Internet Application. Click OK. The project wizard creates two projects:

  • ChatService: This project is the Windows Azure application. It defines the Azure roles and other configuration options.
  • SignalRChat: This project is your ASP.NET MVC 4 project.

Create the SignalR Chat Application

To create the chat application, follow the steps in the tutorial Getting Started with SignalR and MVC 4.

Use NuGet to install the required libraries. From the Tools menu, select NuGet Package Manager, then select Package Manager Console. In the Package Manager Console window, enter the following commands:

Install-Package -ProjectName SignalRChat Microsoft.AspNet.SignalR
Install-Package -ProjectName SignalRChat Microsoft.AspNet.SignalR.ServiceBus

Use the -ProjectName option to install the packages to the ASP.NET MVC project, rather than the Windows Azure project.

Configure the Backplane

In your application's Global.asax file, add the following code:

protected void Application_Start()
{
    string connectionString = "";
    GlobalHost.DependencyResolver.UseServiceBus(connectionString, "Chat");  

    RouteTable.Routes.MapHubs();
}

Now you need to get your service bus connection string. In the Azure portal, select the service bus namespace that you created and click the Access Key icon.

Screenshot of the Create, Access Key, and Delete options and icons in the service bus namespace with a focus on the Create option.

Copy the connection string to the clipboard, then paste it into the connectionString variable.

Screenshot of the Access Key Connect to your namespace screen, showing the Connection String, Default Issuer, and Default Key fields.

string connectionString = "Endpoint=sb://xxxx.servicebus.windows.net/;
SharedSecretIssuer=owner;SharedSecretValue=XXXXXXXX";

Deploy to Azure

In Solution Explorer, expand the Roles folder inside the ChatService project.

Screenshot of the Solution Explorer tree showing the Signal R Chat option contained in the Roles folder of the Chat Service project.

Right-click the SignalRChat role and select Properties. Select the Configuration tab. Under Instances select 2. You can also set the VM size to Extra Small.

Screenshot of the Configuration Tab's Instances section, showing a 2 entered into the Instance count field and the V M Size field set to Extra Small.

Save the changes.

In Solution Explorer, right-click the ChatService project. Select Publish.

Screenshot of the Solution Explorer screen's Chat Service project, with a right-click dropdown menu showing the Publish... option.

If this is your first time publishing to Windows Azure, you must download your credentials. In the Publish wizard, click "Sign in to download credentials". This will prompt you to sign into the Windows Azure portal and download a publish settings file.

Screenshot of the Publish Windows Azure Application screen's Sign In tab with the Sign in to download credentials link being highlighted.

Click Import and select the publish settings file that you downloaded.

Click Next. In the Publish Settings dialog, under Cloud Service, select the cloud service that you created earlier.

Screenshot of hte Publish Windows Azure Application screen's Settings tab, showing the Cloud Service field in the Common Settings tab.

Click Publish. It can take a few minutes to deploy the application and start the VMs.

Now when you run the chat application, the role instances communicate through Azure Service Bus, using a Service Bus topic. A topic is a message queue that allows multiple subscribers.

The backplane automatically creates the topic and the subscriptions. To see the subscriptions and message activity, open the Azure portal, select the Service Bus namespace, and click on "Topics".

Screenshot of the selected Cloud Service field now populating in the Azure portal, with the Cloud Service's Name field being highlighted.

It make take a few minutes for the message activity to show up in the dashboard.

Screenshot of the Azure portal dashboard displaying message activity timeline, showing a blue and purple line to indicate different message histories.

SignalR manages the topic lifetime. As long as your application is deployed, don't try to manually delete topics or change settings on the topic.