Using SignalR with Kentico

Login to vote
We recently used the new SignalR library with Kentico to create a realtime "wall" for the MS Society's new Micro-site, The library creates an asynchronous connection with the server, allowing the server to push changes to the client. When a user adds a "Connection" to the wall all browsers are updated at the same time, making the page a bit more dynamic and a bit more social. If an administrator of the site is logged in and on the page, they are able to watch live analytics of the page’s use. SignalR makes building dynamically updated content a breeze. Getting SignalR setup with Kentico isn’t terribly straightforward so we decided to document the process so you can get up and running with this awesome technology (Note: Hotfix 30 made this quite a bit more straightforward). This tutorial walks you through setting up Kentico and SignalR with the Sample Stock Ticker project in a localhost environment. You might want to follow a simpler signalr tutorial to get used to how signalr works before adding it to Kentico.

  • Visual Studio (I'm using 2010)
  • .NET Framework 4
  • a Kentico installation.
Note: It's not as much of an issue until you move into production with a SignalR installation, but if you want to make use of sockets, you'll also need Windows Server 2012 and IIS 8

  1. Install Kentico 7 if you haven't, set up your database, get your license key, etc. -
  2. Install NuGet for Visual Studio if you don't have it (this makes adding third party extensions super simple). -
  3. Open the Website in Visual Studio.
  4. Before running, I recommend changing some settings to only build pages when necessary. Go to View > Property Pages > Build. Configure to only build on each page like so:

  5. Go to Website and choose "User IIS Express" to run it locally with IIS Express.
  6. Open NuGet. Go to Tools > Library Package Manager > Manage NuGet Packages for Solution.
  7. Search for Microsoft.AspNet.SignalR and install the first two on the page - Microsoft.AspNet.SignalR and Micosoft.AspNet.SignalR.Sample

  8. If you start the site, you should receive a nice little error. Kentico and SignalR include the same ajax classes. In the site's bin folder, right-click AjaxMin.dll and choose Exclude from project. This simply adds .exclude to the end of the file so it does not get compiled.
  9. SignalR needs access to the folders /signalr and /bundles, so we need to exclude these urls from Kentico. In the Kentico CMS, Go to Site Manager > Settings > URLs and SEO, and add /signalr;/bundles to the Excluded URLs field and click save.
  10. In App_Code\App_Start|RegisterHubs.cs, choose to register the hubs on the PostApplicationStartupMethod.Change this line:
    [assembly: WebActivatorEx.PreApplicationStartMethod(typeof(SignalR.StockTicker.RegisterHubs), "Start")]
    [assembly: WebActivatorEx.PostApplicationStartMethod(typeof(SignalR.StockTicker.RegisterHubs), "Start")]

    NOTE: As of Kentico 7 Hotfix 30, it is no longer necessary to exclude custom route URLs (such as '/SignalR') from the CMS rewriting engine, which eliminates the issue noted below, so if you have applied hotfix 30 to your Kentico 7 installation, you should be able to skip step 11.
  11. Click Debug > Start Debugging and visit /SignalR.Sample/StockTicker.html. Visual Studio should catch an error found in App_Code/Applications/CMSAppBase.cs around line 897 within the method CMSPreSendRequestHeaders. For some reason the signalr connection does something goofy and the url is either not being set or is being set to null. We can fix this error by adding URLHelper.Url != null to the statement. It should look like this when you're done:
  12. Click Open Market in the ticker example. You should see a bunch of asynchronous goodness.
  • When clearing the Kentico site cache, be sure to restart the application. Clearing the cache removes the generated hub files, which essentially removes your signalr services. Restarting the application will fire the WebActivatorEx.PostApplicationStartMethod() and recreate your hubs.
Posted by Adam Hass on 7/22/2013 9:53:51 AM
Blog post currently doesn't have any comments.