Wiring SignalR with Autofac

Tags: autofac, signalr, english

After having a bit of struggle recently (which was quite fun, btw) with making two cool software pieces – Orchard and SignalR – work together, I came up with a simple library that allows you to wire SignalR’s internal dependency injection mechanism with Autofac. Useful thing if you already use Autofac and wish to keep all the dependencies there.

Orchard uses Autofac, so it was pretty obvious that something like this would be necessary at some point to make everything work.

The code is available at GitHub: https://github.com/pszmyd/SignalR.Autofac. I’ll publish the lib as NuGet package shortly.


It’s simple as hell – just a single class. To use it in your ASP.NET app, you need to set the SignalR resolver to AutofacDependencyResolver in your bootstrapping/initialization code:

var resolver = new AutofacDependencyResolver(lifetimeScope); 

It takes a single ctor argument – your Autofac container instance (ILifetimeScope).

The simplest example could look like this (assuming Initialize is a method that bootstraps your application):

public void Initialize() { 
    // Building the Autofac container 
    var builder = new ContainerBuilder(); 
    // .... 

    var container = builder.Build(); 
    var resolver = new AutofacDependencyResolver(container); 

And that’s all. Now, you can use all the types that SignalR registers (eg. IConnectionManager) in other parts of your application and vice-versa.


  • Rafal said

    I want to add Orchard module which utilizes SignalR. Could you tell: 1. How to obtain the lifetimeScope (object to pass to AutofacDependencyResolver constructor) in Orchard module ? 2. What is a good place to put initialization code in Orchard module?

  • pszmyd said

    @Rafal Good questions.

    1. Add a class implementing Autofac.Module in your Orchard module and add appropriate registration in there. Like this:

    protected override void Load(ContainerBuilder moduleBuilder) { moduleBuilder.Register(ctx => { var resolver = new AutofacDependencyResolver(ctx.Resolve()); return resolver; }) .As() .InstancePerMatchingLifetimeScope("shell"); }

    1. Implement IOrchardShellEvents in your module and put it inside Activate method. This method is called when a tenant is about to start.

    Wiring SignalR with Orchard is very tricky, because of it's highly async architecture (NHibernate doesn't like it) and Orchard routing system. It's doable, although far too complex to describe in comment or even a single post. I'm writing a module for that right now - it will be public so you could try it yourself:)

  • Will said

    Do you have anything for the updated SignalR? I tried this and it didn't seem to work.

  • pszmyd said

    Demoed the pre-release version last week on Orchard Commitee meeting and it worked very fine.

    Need to do some refactoring and remove lots of trash used for testing. I guess it will arrive online somewhere around the end of this week.

  • Bob said

    Awesome news, have been aching to play with SignalR in orchard for a while now. Cant wait :)

  • Christian Weyer said

    I have forked the code (https://github.com/thinktecture/SignalR.Autofac), updated it and created a NuGet package. https://nuget.org/packages/SignalR.AutoFac/

    Please let me know whether this is fine for you.

  • marioz said

    any news about the signalr orchard module? I think it could be a very interesting module

  • Leo said

    Cheers for the article, it's very useful. I'm still learning SignalR with Autofac and running into lots of problems. It would be great if you have a look at my post on stackoverflow.