Skip to content

Extensibility

davidfowl edited this page Feb 5, 2013 · 30 revisions

Extensiblity

SignalR is built with dependency injection in mind. You can replace most of SignalR pieces with your own implementations and even replace the IDependencyResolver with one of your own. If you're familiar with DI in ASP.NET MVC, then it should feel similar.

Replacing individual components

You can replace individual parts of SignalR without replacing the DependencyResolver by calling

GlobalHost.DependencyResolver.Register(type, Func<object>):
GlobalHost.DependencyResolver.Register(typeof(IConnectionIdPrefixGenerator), () => new CustomConnectionIdPrefixGenerator());

Replaceable components

The following lists the pluggable interfaces in SignalR.

  • IMessageBus - Message bus.
  • IAssemblyLocator - Locates assemblies to find hubs in.
  • IJavaScriptProxyGenerator - Generates the client proxy for hubs.
  • IJavaScriptMinifier - Allows the dynamic javascript proxy to be minified.
  • IJsonSerializer - Used to serialize and deserialze outgoing/incoming data.

Replacing the IJsonSerializer

If you want to add custom serialization logic, you can replace the default IJsonSerializer. This may be useful for doing things like changing the default date format or using custom serialization settings.

NOTE: SignalR's clients are case sensitive so do not change the case of members when serializing. You can register this custom serializer in Global.asax.

public class Global : System.Web.HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        var serializer = new JsonNetSerializer(new JsonSerializerSettings
        {
            DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
            NullValueHandling = NullValueHandling.Ignore
        });

        GlobalHost.DependencyResolver.Register(typeof(IJsonSerializer), () => serializer); 
    }
}

Replacing the DependencyResolver

You can change the DependencyResolver to use your DI container of choice by setting GlobalHost.DependencyResolver.

NOTE: DO NOT override the global resolver in PreApplicationStart, it will not work, or it'll work only sometimes. Do it in PostApplicationStart (using WebActivator) or in Global.asax.

public class Global : System.Web.HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        GlobalHost.DependencyResolver = new CustomResolver();
 
        RouteTable.Routes.MapHubs();
    }
}

Here, we're changing the global resolver and remapping the default hub route to pick it up.

NOTE: The hub route is automatically added at a very early stage in the application (PreApplicationStart) so we need to remap it to use the new default resolver.

We can also specify a new one for the hub route directly:

public class Global : System.Web.HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.MapHubs(new CustomResolver());
    }
}

NOTE: When using ASP.NET MVC, configure SignalR first, then ASP.NET MVC

public class Global : System.Web.HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
      GlobalHost.DependencyResolver = new CustomResolver();
      RouteTable.Routes.MapHubs();
            
      AreaRegistration.RegisterAllAreas();
      RegisterGlobalFilters(GlobalFilters.Filters);
      RegisterRoutes(RouteTable.Routes);
    }
}