title: Autofac injection of IHubContext SignalR MVC
body: <p>I am trying to get SignalR working with Autofac. I have a repo of a stripped back version of what I have done here:</p>
  
  <p><a href="https://github.com/justsayno/signalr-autofac" rel="nofollow">https://github.com/justsayno/signalr-autofac</a></p>
  
  <p>This is adapted from which works using <code>GlobalHost</code>:</p>
  
  <p><a href="https://github.com/sstorie/experiments/tree/master/angular2-signalr" rel="nofollow">https://github.com/sstorie/experiments/tree/master/angular2-signalr</a></p>
  
  <p>That works just fine using hte GlobalHost object. I have attempted to follow the documentation on Autofac's site about how to inject SignalR services but I cannot get it to work. This is my config file for registering my dependencies:</p>
  
  <pre><code>public static IContainer RegisterDependencies()
      {
          // Create your builder.
          var builder = new ContainerBuilder();
  
          //register controllers in DI
          builder.RegisterControllers(Assembly.GetExecutingAssembly());
          builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
  
          // Register SignalR hubs
          builder.RegisterHubs(Assembly.GetExecutingAssembly());
  
          return builder.Build();
      }
  </code></pre>
  
  <p>And I call this from startup.cs:</p>
  
  <pre><code>public class Startup
  {
      public static IContainer Container { get; set; }
      public void Configuration(IAppBuilder app)
      {
          var config = GlobalConfiguration.Configuration;
  
          // configure IoC container
          Container = AutofacConfiguration.RegisterDependencies();
  
          //set dependency resolver from WebAPI and MVC
          config.DependencyResolver = new AutofacWebApiDependencyResolver(Container);
          DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(Container));
  
          //register Autofac Middleware
          app.UseAutofacMiddleware(Container);
  
          app.Map("/signalr", a =&gt;
          {
              a.UseCors(CorsOptions.AllowAll);
              var hubConfiguration = new HubConfiguration
              {
                  Resolver = new Autofac.Integration.SignalR.AutofacDependencyResolver(Container)
              };
              a.RunSignalR(hubConfiguration);
          });
  
          // This server will be accessed by clients from other domains, so
          //  we open up CORS
          //
          app.UseCors(CorsOptions.AllowAll);
  
          // Build up the WebAPI middleware
          //
          var httpConfig = new HttpConfiguration();
          httpConfig.MapHttpAttributeRoutes();
          app.UseWebApi(httpConfig);
      }
  
  }
  </code></pre>
  
  <p>And I have a controller that has this injected into its contstructor:</p>
  
  <pre><code> public TaskController(IHubContext hubContext)
      {
          // Commented out hard coded global host version
          //
          //_context = GlobalHost.ConnectionManager.GetHubContext&lt;EventHub&gt;();
  
          // using DI
          _context = hubContext;
      }
  </code></pre>
  
  <p>This however gives me an error about the controller not having a default constructor (so I assume this is a problem with my IHubContext not being found).</p>
  
  <p>Any help would be great. I have made a repo of what I am talking about that can be found here for a complete solution:</p>
  
  <p><a href="https://github.com/justsayno/signalr-autofac" rel="nofollow">https://github.com/justsayno/signalr-autofac</a></p>