Skip to content

Latest commit

 

History

History
116 lines (83 loc) · 5.84 KB

File metadata and controls

116 lines (83 loc) · 5.84 KB

Run ASP.NET SignalR

If it is your first time trying SignalR, we recommend you to use the ASP.NET Core SignalR, it is simpler, more reliable, and easier to use.

1. Install and Use Service SDK

Install SignalR Service SDK to your ASP.NET project with Package Manager Console:

Install-Package Microsoft.Azure.SignalR.AspNet

In your Startup class, use SignalR Service SDK as the following code snippet, replace MapSignalR() to MapAzureSignalR({your_applicationName}). Replace {YourApplicationName} to the name of your application, this is the unique name to distinguish this application with your other applications. You can use this.GetType().FullName as the value.

public void Configuration(IAppBuilder app)
{
    app.MapAzureSignalR(this.GetType().FullName);
}

2. Configure Connection String

Set the connection string in the web.config file, to the connectionStrings section:

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

3. Configure Service Options

There are a few options you can customize when using Azure SignalR Service SDK.

ConnectionString

  • Default value is the Azure:SignalR:ConnectionString connectionString or appSetting in web.config file.
  • It can be reconfigured, but please make sure the value is NOT hard coded.

InitialHubServerConnectionCount

  • Default value is 5.
  • This option controls the initial count of connections per hub between application server and Azure SignalR Service. Usually keep it as the default value is enough. During runtime, the SDK might start new server connections for performance tuning or load balancing. When you have big number of clients, you can give it a larger number for better throughput. For example, if you have 100,000 clients in total, the connection count can be increased to 10 or 15.

MaxHubServerConnectionCount

  • Default value is null.
  • This option controls the max count of connections allowed per hub between application server and Azure SignalR Service. During runtime, the SDK might start new server connections for performance tuning or load balancing. By default a new server connection starts whenever needed. When the max allowed server connection count is configured, the SDK does not start new connections when server connection count reaches the limit.

ApplicationName

  • Default value is null.
  • This option can be useful when you want to share the same Azure SignalR instance for different app servers containing the same hub names. If not set, all the connected app servers are considered to be instances of the same application.

ClaimProvider

  • Default value is null.
  • This option controls what claims you want to associate with the client connection. It will be used when Service SDK generates access token for client in client's negotiate request. By default, all claims from IOwinContext.Authentication.User of the negotiate request will be reserved.
  • Normally you should leave this option as is. Make sure you understand what will happen before customizing it.

AccessTokenLifetime

  • Default value is 1 hour.
  • This option controls the valid lifetime of the access token, which is generated by Service SDK for each client. The access token is returned in the response to client's negotiate request.
  • When ServerSentEvent or LongPolling is used as transport, client connection will be closed due to authentication failure after the expire time. You can increase this value to avoid client disconnect.

AccessTokenAlgorithm

  • Default value is HS256
  • This option provides choice of SecurityAlgorithms when generate access token. Now supported optional values are HS256 and HS512. Please note HS512 is more secure but the generated token will be comparatively longer than that using HS256.

ServerStickyMode

MaxPollIntervalInSeconds

  • Default value is 5
  • This option defines the max poll interval allowed for LongPolling connections in Azure SignalR Service. If the next poll request does not come in within MaxPollIntervalInSeconds, Azure SignalR Service cleans up the client connection. Note that Azure SignalR Service will also clean up connections when cached waiting to write buffer size is greater than 1Mb to ensure service performance.
  • The value is limited to [1, 300].

Sample

You can configure above options like the following sample code.

app.Map("/signalr",subApp => subApp.RunAzureSignalR(this.GetType().FullName, new HubConfiguration(), options =>
{
    options.InitialHubServerConnectionCount = 1;
    options.AccessTokenLifetime = TimeSpan.FromDays(1);
    options.ClaimProvider = context => context.Authentication?.User.Claims;
}));