The SignalR team provides a third backplane option, based on Microsoft SQL Server. This one could be a good fit if both Windows Azure and Redis are not an option for you. It has good resilience characteristics, although it's not the best option in terms of performance.
In this recipe, we'll assume you have a SQL Server instance available, and we'll see how to plug the SQL Server backplane into a simple application inspired by the previous scale-out recipes.
As we did previously, we'll reuse the code we wrote in the recipe about the Windows Azure Service Bus backplane, and we'll just show what's to be different when we add the SQL Server backplane. We'll perform our demonstration using a new empty web application named Recipe40
.
In order to correctly support the backplane, we need to perform the following actions:
Our sample code will be the same as we wrote for Recipe38
; let's quickly review it:
EchoHub
and a test page called index.html
, whose content will be the same as we had prepared for the previous two recipes.Startup
class that looks like the following code:using Microsoft.Owin; using Owin; [assembly: OwinStartup(typeof(Recipe40.Startup))] namespace Recipe40 { public class Startup { public void Configuration(IAppBuilder app) { ... app.MapSignalR(); } } }
We'll be adding the missing code later when plugging the SQL Server backplane.
We can proceed with a first test pass by opening the index.html
page in multiple browser windows and by clicking on the button on each of them to verify that messages are broadcasted across clients. When done, we can proceed by adding the SQL Server backplane using the following steps:
Microsoft.AspNet.SignalR.SqlServer
NuGet package to enable support for the SQL Server backplane.Startup
class code, where we first add a using Microsoft.AspNet.SignalR;
directive and then the following call just before invoking the MapSignalR()
method:string connectionString = "[connection string]"; GlobalHost.DependencyResolver.UseSqlServerSqlServer(connectionString);
This code is pretty simple: the UseSqlServerSqlServer()
method starts the backplane, and we just have to provide the necessary connection string to have access to the database we created earlier.
We are ready to perform a new test on the application by launching it again from Visual Studio in multiple browser windows, and we should not notice any difference from the behavior we observed earlier, with the messages correctly broadcasted across all the clients. You can double-check that the backplane is effectively in place by inspecting the content of the tables in its database.
We can then perform a further test to clearly observe how the backplane works across multiple servers. We open a new command-line window, and from there we launch an instance of IIS Express
pointing at our application:
"%programfiles%IIS Expressiisexpress.exe" /path:[path] /port:[port]
As we did in the previous recipe, we are starting a new web server instance that will publish our application: the [path]
argument must be the absolute path to the folder of our application, and the [port]
value must be a free IP port. When the new IIS Express
instance is started, we can open a new browser window pointing at the port we just specified, and a new copy of our test page will be loaded. Regardless of being on a different web server instance, any message sent from here will reach the other windows we opened earlier, and vice versa, thanks to the SQL Server backplane.