2015-01-26

In order to properly integrate SignalR with a SharePoint 2013 farm solution you have to make two types of changes to the typical SignalR boilerplate code: Change the Startup.cs and make a couple of web.config modifications.

Please note that this sample assumes that you are using SignalR 2.2, which in turn references OWin 2.1. In case you are using a newer version, make sure to adapt the assembly version numbers in the code snippets below accordingly (Version=2.1.0.0).

Startup.cs class

Add the DefaultAssemblyLocator to your Startup.cs, it is needed to locate and load your hub class in SharePoint:

GlobalHost.DependencyResolver.Register(typeof(IAssemblyLocator),() => new DefaultAssemblyLocator());

A sample Startup.cs would look like this:

public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
GlobalHost.DependencyResolver.Register(typeof(IAssemblyLocator),() => new DefaultAssemblyLocator());
 }
 }

SharePoint web.config modifications

In your web.config, make the following changes (use SPWebConfigModification for a proper deployment):

a) App settings

<appSettings>
(…)
<add key="owin:appStartup" value="YourNamespace.YourNameOfStartupClass, YourDllName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXX" />
<add key="owin:AutomaticAppStartup" value="true" />
</appSettings>

b) Add the OWin handler, so that the hub path is handled by Owin/SignalR (e.g. “/signalr/hubs”)

c) Remove or comment the “ExtensionlessUrl” entries in the handlers section. This is needed so that IIS can handle the hub path at all (e.g. “/signalr/hubs”)

<handlers>
(…) 
<add name="Owin" verb="*" path="/signalr" type="Microsoft.Owin.Host.SystemWeb.OwinHttpHandler, Microsoft.Owin.Host.SystemWeb, Version=2.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<!-- remove name="ExtensionlessUrl-ISAPI-4.0_64bit" / -->
<!-- remove name="ExtensionlessUrl-ISAPI-4.0_32bit" / -->
(…) 
</handlers>

d) Change the legacyCasModel to FALSE

<trust level="Full" originUrl="" legacyCasModel="false" />

e) Optional: If there is already a version of OWin installed, make sure to also add an assembly binding redirect to load the new versions:

<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.1.0.0" newVersion="2.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.1.0.0" newVersion="2.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>

Final points

Verify in your application page or custom web part that the order of the JavaScript includes is as follows:

<script src="Scripts/jquery-1.8.2.min.js" ></script>
<script src="Scripts/jquery.signalR-2.2.0.min.js"></script>
<script src="/signalr/hubs"></script>

…and you are good to go!

Alternative

An alternative Approach would be to generate the JavaScript file once. Look in the SignalrR docs for “How to create a physical file for the SignalR generated Proxy”.

*** This post has been originally posted on my blog ***

About the author 

Oliver Pistor