title: Autofac DependencyResolutionException for ILifetimeScope for a hub in asp.net mvc, signalr, MS owin
body: <p>What should be the Autofac 3.5 configuration for Asp.net Mvc 5.2, SignalR 2.1, MS Owin (Katana) 3.0? Is there less complex way to register Autofac resolvers (there is two of them now)? Or why <code>ILifetimeScope</code> is not visible for my hub?</p>
  
  <p>The exception:</p>
  
  <blockquote>
    <p>Autofac.Core.DependencyResolutionException: An exception was thrown
    while invoking the constructor 'Void .ctor(Autofac.ILifetimeScope)' on
    type 'OperatorHub'. --->  </p>
    
    <p>No scope with a Tag matching
    'AutofacWebRequest' is visible from the scope in which the instance
    was requested. This generally indicates that a component registered as
    per-HTTP request is being requested by a SingleInstance() component
    (or a similar scenario.) Under the web integration always request
    dependencies from the DependencyResolver.Current or
    ILifetimeScopeProvider.RequestLifetime, never from the container
    itself. (See inner exception for details.) ---> </p>
    
    <p>Autofac.Core.DependencyResolutionException: No scope with a Tag
    matching 'AutofacWebRequest' is visible from the scope in which the
    instance was requested. This generally indicates that a component
    registered as per-HTTP request is being requested by a
    SingleInstance() component (or a similar scenario.) Under the web
    integration always request dependencies from the
    DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime,
    never from the container itself.</p>
  </blockquote>
  
  <p>In my OwinStartup (see <a href="http://autofac.readthedocs.org/en/latest/integration/mvc.html#owin-integration" rel="nofollow">autofac + mvc owin</a> and <a href="http://autofac.readthedocs.org/en/latest/integration/signalr.html#owin-integration" rel="nofollow">autofac + signalr in owin</a>):</p>
  
  <pre><code>public void Configuration(IAppBuilder app)
  {
      var builder = new ContainerBuilder();
  
      // ... registration. There is .InstancePerRequest() and .SingleInstance()
  
      Autofac.Integration.Mvc.RegistrationExtensions.RegisterControllers(builder,typeof(MvcApplication).Assembly);
      Autofac.Integration.SignalR.RegistrationExtensions.RegisterHubs(builder, Assembly.GetExecutingAssembly());
  
      var container = builder.Build();
  
      // 1st resolver
      DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
  
      app.UseAutofacMiddleware(container);
      app.UseAutofacMvc();
  
      // yet the 2nd resolver!
      app.MapSignalR(new HubConfiguration { Resolver = new Autofac.Integration.SignalR.AutofacDependencyResolver(container) });
  }
  </code></pre>
  
  <p>The hub:</p>
  
  <pre><code>public class OperatorHub : Hub
  {
      public OperatorHub(ILifetimeScope hubLifetimeScope) 
      {
          hubLifetimeScope.BeginLifetimeScope(); 
          // ...
  
          // HERE IT FALLS. The IMyService relates to MyDbContext (see below)
          var myservice = hubLifetimeScope.Resolve&lt;IMyService&gt;();
  
      }
  }
  </code></pre>
  
  <p>UPDATE</p>
  
  <p>The breaking component registration (EF Context:</p>
  
  <pre><code>        builder.RegisterType&lt;MyDbContext&gt;().AsSelf().As&lt;DbContext&gt;().InstancePerRequest("OwinLifetimeScope");
  </code></pre>
  
  <p>In short the bug is the <code>MyDbContext</code> is not in the 'root' lifetime scope which is passed to <code>OperatorHub</code> constructor.</p>
  
  <p>UPDATE 2</p>
  
  <p>The solution with the help of @TravisIllig is to register the MyDbContext service using <code>.InstancePerLifetimeScope()</code> and to create the one in the hub. Another lifetime scope would be created for http request in asp mvc. Create help at <a href="http://autofac.readthedocs.org/en/latest/faq/per-request-scope.html#sharing-dependencies-across-apps-without-requests" rel="nofollow">Sharing Dependencies Across Apps Without Requests</a>.</p>
  
  <p>Also the hub <em>should not</em> dispose the given scope as it is the root one which results in <code>ObjectDisposedException</code> on the second run.</p>