title: How can I use Autofac in EndRequest?
body: <p>I'm using Autofac with .Net MVC 3. It seems that Autofac disposes of the lifetime scope in Application_EndRequest, which makes sense. But that's causing this error when I try to find a service in my own code that executes during EndRequest:</p>
  
  <pre><code> MESSAGE: Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it has already been disposed.
  STACKTRACE: System.ObjectDisposedException
    at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)
    at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable`1 parameters)
    at System.Web.Mvc.DependencyResolverExtensions.GetService[TService](IDependencyResolver resolver)
  </code></pre>
  
  <p>For reference, here's the code I'm using to try to resolve services:</p>
  
  <pre><code>    public T GetInstance&lt;T&gt;()
      {
          return DependencyResolver.Current.GetService&lt;T&gt;();
      }
  </code></pre>
  
  <p>Is there any way I can have code that is executed from EndRequest take advantage of Autofac for service resolution?</p>
  
  <p>Edit: I thought of doing it with a separate scope just during EndRequest, as Jim suggests below. However, this means any services that are registered as <code>InstancePerHttpRequest</code> will get a new instance during EndRequest, which seems non-ideal. </p>