|
| 1 | +# IntelliTect.AspNetCore.SignalR.SqlServer |
| 2 | + |
| 3 | +A Microsoft SQL Server backplane for ASP.NET Core SignalR. |
| 4 | + |
| 5 | +This project is largely based off of a fork of the [SignalR Core Redis provider](https://github.com/dotnet/aspnetcore/tree/main/src/SignalR/server/StackExchangeRedis), reworked to use the underlying concepts of the [classic ASP.NET SignalR SQL Server backplane](https://github.com/SignalR/SignalR/tree/main/src/Microsoft.AspNet.SignalR.SqlServer). This means it supports subscription-based messaging via SQL Server Service Broker, falling back on periodic polling when not available. |
| 6 | + |
| 7 | + |
| 8 | +## SQL Server Configuration |
| 9 | + |
| 10 | +For optimal responsiveness and performance, [SQL Server Service Broker](https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/sql-server-service-broker?view=sql-server-ver15) should be enabled on your database. NOTE: **Service Broker is not available on Azure SQL Database.** If you are running in Azure, consider a Redis or Azure SignalR Service backplane instead. |
| 11 | + |
| 12 | +If Service Broker is not available, a fallback of periodic queries is used. This fallback querying happens very rapidly when messages are encountered, but slows down once traffic stops. |
| 13 | + |
| 14 | +You can check if Service Broker is enabled with the following query: |
| 15 | +``` sql |
| 16 | +SELECT [name], [service_broker_guid], [is_broker_enabled] |
| 17 | +FROM [master].[sys].[databases] |
| 18 | +``` |
| 19 | + |
| 20 | +To enable it, execute the following against the database. Note that this requires an exclusive lock over the database: |
| 21 | +``` sql |
| 22 | +ALTER DATABASE [DatabaseName] SET ENABLE_BROKER WITH NO_WAIT; |
| 23 | +``` |
| 24 | + |
| 25 | +If the above command does not work due to existing connections, try terminating existing sessions automatically using |
| 26 | +``` sql |
| 27 | +ALTER DATABASE [DatabaseName] SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE |
| 28 | +``` |
| 29 | + |
| 30 | +You can also set `AutoEnableServiceBroker = true` when configuring in your `Startup.cs`, but this requires that the application have permissions to do so and has the same caveats that there can be no other active database sessions. |
| 31 | + |
| 32 | +## Usage |
| 33 | + |
| 34 | +1. Install the `IntelliTect.AspNetCore.SignalR.SqlServer` NuGet package. |
| 35 | +2. In `ConfigureServices` in `Startup.cs`, configure SignalR with `.UseSqlServer()`: |
| 36 | + |
| 37 | + |
| 38 | +Simple configuration: |
| 39 | +``` cs |
| 40 | +services |
| 41 | + .AddSignalR() |
| 42 | + .AddSqlServer(Configuration.GetConnectionString("Default")); |
| 43 | +``` |
| 44 | + |
| 45 | +Advanced configuration: |
| 46 | + |
| 47 | +``` cs |
| 48 | +services |
| 49 | + .AddSignalR() |
| 50 | + .AddSqlServer(o => |
| 51 | + { |
| 52 | + o.ConnectionString = Configuration.GetConnectionString("Default"); |
| 53 | + // See above - attempts to enable Service Broker on the database at startup |
| 54 | + // if not already enabled. Default false, as this can hang if the database has other sessions. |
| 55 | + o.AutoEnableServiceBroker = true; |
| 56 | + // Every hub has its own message table(s). |
| 57 | + // This determines the part of the table named that is derived from the hub name. |
| 58 | + // IF THIS IS NOT UNIQUE AMONG ALL HUBS, YOUR HUBS WILL COLLIDE AND MESSAGES MIX. |
| 59 | + o.TableSlugGenerator = hubType => hubType.Name; |
| 60 | + // The number of tables per Hub to use. Adding a few extra could increase throughput |
| 61 | + // by reducing table contention, but all servers must agree on the number of tables used. |
| 62 | + // If you find that you need to increase this, it is probably a hint that you need to switch to Redis. |
| 63 | + o.TableCount = 1; |
| 64 | + // The SQL Server schema to use for the backing tables for this backplane. |
| 65 | + o.SchemaName = "SignalRCore"; |
| 66 | + }); |
| 67 | +``` |
| 68 | + |
| 69 | +Alternatively, you may configure `IntelliTect.AspNetCore.SignalR.SqlServer.SqlServerOptions` with [the Options pattern](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-5.0). |
| 70 | + |
| 71 | +``` cs |
| 72 | +services.Configure<SqlServerOptions>(Configuration.GetSection("SignalR:SqlServer")); |
| 73 | +``` |
| 74 | + |
| 75 | +## Caveats |
| 76 | + |
| 77 | +* As mentioned above, if SQL Server Service Broker is not available, messages will not always be transmitted immediately since a fallback of periodic querying must be used. |
| 78 | +* This is not the right solution for applications with a need for very high throughput, or very high degrees of scale-out. Consider Redis or Azure SignalR Service instead for such cases. You should always do an appropriate amount of testing to determine if a particular solution is suitable for your application. |
0 commit comments