title: Managing AutoFac lifetime scopes per session and request in asp.net mvc 3
body: <p>I want to use AutoFac in a web application. I have the root container, a child container per session and a child container per request. I'm trying to figure out what the/a best way is to manage these lifetime scopes. In the Global.asax.cs I have added the following:</p>
  
  <pre><code>protected void Application_Start(object sender, EventArgs e)
  {
      var container = ...;
  }
  
  protected void Session_Start(object sender, EventArgs e)
  {
      var sessionScope = container.BeginLifetimeScope("session");
  
      Session["Autofac_LifetimeScope"] = sessionScope;
  }
  
  protected void Application_BeginRequest(object sender, EventArgs e)
  {
      var sessionScope = (ILifetimeScope) Session["Autofac_LifetimeScope"];
      var requestScope = sessionScope.BeginLifetimeScope("httpRequest");
  
      HttpContext.Current.Items["Autofac_LifetimeScope"] = requestScope;
  }
  
  protected void Application_EndRequest(object sender, EventArgs e)
  {
      var requestScope = (ILifetimeScope)HttpContext.Current.Items["Autofac_LifetimeScope"];
      requestScope.Dispose();
  }
  
  protected void Session_End(object sender, EventArgs e)
  {
      var sessionScope = (ILifetimeScope)Session["Autofac_LifetimeScope"];
  
      sessionScope.Dispose();
  }
  
  protected void Application_End(object sender, EventArgs e)
  {
      container.Dispose();
  }
  </code></pre>
  
  <ol>
  <li><p>How can I tell AutoFac to use my requestScope as the starting point for getting dependencies, so that the implementations I register as InstancePerLifetimeScope will be resolved using my requestScope?</p></li>
  <li><p>If that is not possible, can I get AutoFac to create its per-request lifetime scope out of my sessionScope?</p></li>
  <li><p>Or am I on the wrong track here? Could there be an other way of making AutoFac aware of this hierarchy?</p></li>
  </ol>
  
  <p>Any help or other comments are appreciated.</p>
  
  <hr>
  
  <p>In response to Steven.</p>
  
  <p>I'm still in the early stages of prototyping, but possible things you could have in the sessionScope:</p>
  
  <ul>
  <li>UserPreferences</li>
  <li>Authentication and authorization context (e.g. user identity and roles)</li>
  </ul>
  
  <p>Not related to the application I'm going to build, but in a e-commerce environment, the shopping cart could be session scoped. This is probably the best concrete example. It is something that you expect to live longer than a request, but shorter than the application.</p>
  
  <p>There could be more than this, but if I have a strategy for the UserPreferences, Authentication and Authorization, then that strategy could also be applied to other components that will be created later.</p>
  
  <p>A possible alternative is to get all the necessary information at the beginning of the request and place these configured components in the request scope. It will give me the result I expect, but it doesn't match the model I have in my mind about application->session->request hierarchy. I'm hoping to create a system that makes sense, since I'm definitely not the one that is going to maintain it.</p>