title: OWIN + SignalR + Autofac
body: <p>Taken from: <a href="http://docs.autofac.org/en/latest/integration/signalr.html" rel="noreferrer">http://docs.autofac.org/en/latest/integration/signalr.html</a>:</p>
  
  <p>"A common error in OWIN integration is use of the GlobalHost. In OWIN you create the configuration from scratch. You should not reference GlobalHost anywhere when using the OWIN integration."</p>
  
  <p>That sounds reasonable. However, how should one resolve <code>IHubContext</code> from an ApiController, like the usual (non-OWIN):</p>
  
  <p><code>GlobalHost.ConnectionManager.GetHubContext&lt;MyHub&gt;()</code>?</p>
  
  <p>I can't find a reference on this one anywhere, and the only method I have by now is to register the <code>HubConfiguration</code> instance within the same container and do this:</p>
  
  <pre><code>public MyApiController : ApiController {
    public HubConfiguration HubConfig { get; set; } // Dependency injected by
                                                    // PropertiesAutowired()
  
    public IHubContext MyHubContext { 
      get { 
        return HubConfig
          .Resolver
          .Resolve&lt;IConnectionManager&gt;()
          .GetHubContext&lt;MyHub&gt;(); 
       } 
    }
  
    // ...
  
  }
  </code></pre>
  
  <p>However, this seems quite verbose to me. What is the proper way to do it? To be more specific, is there a clean way to register <code>IConnectionManager</code>?</p>
  
  <p>EDIT:</p>
  
  <p>What I ended up doing is something like:</p>
  
  <pre><code>var container = builder.Build();
  hubConfig.Resolver = new AutofacDependencyResolver(container); 
  
  app.MapSignalR("/signalr", hubConfig);
  
  var builder2 = new ContainerBuilder();
  builder2
    .Register(ctx =&gt; hubConfig.Resolver.Resolve&lt;IConnectionManager&gt;())
    .As&lt;IConnectionManager&gt;();
  
  builder2.Update(container);
  </code></pre>
  
  <p>but I have a feeling there must be an easier way to get that <code>IConnectionManager</code> injected in the controller.</p>