title: Autofac - The request lifetime scope cannot be created because the HttpContext is not available - due to async code?
body: <p>Short Question: <a href="https://stackoverflow.com/q/15050207/1267778">Same as this unanswered problem</a> </p>
  
  <p>Long Question:</p>
  
  <p>I just ported some code over from an MVC 4 + Web Api solution that was using Autofac into my new solution which is also using Autofac but only with Web Api 2 (no MVC 5.1 project, just a web api).</p>
  
  <p>In my previous solution I had MVC4 and Web Api so I had 2 Bootstrapper.cs files, one for each. I copied over just the Web Api bootstrapper for the new project.</p>
  
  <p>Now I have 2 other projects in the new solution that need to pull a dependency. Lets just assume I have to use <code>DependencyResolver.Current.GetService&lt;T&gt;()</code> despite it being an anti-pattern.</p>
  
  <p>At first this was not working until I set the MVC Dependency Resolver to the same container:</p>
  
  <pre><code>GlobalConfiguration.Configuration.DependencyResolver = 
       new AutofacWebApiDependencyResolver(container);
  
  //I had to pull in Autofac.Mvc and Mvc 5.1 integration but this line fixed it
  DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
  </code></pre>
  
  <p>The strange part is, doing that only fixed it in ONE of those projects! Here's the situation:</p>
  
  <pre><code> Solution.Web project
        Bootstrapper.cs that registers both dependency resolvers for web api and mvc.
  
   Solution.ClassLib project
        var userRepo = DependencyResolver.Current.GetService&lt;IUserRepo&gt;(); //Good! :)
  
   Solution.WindowsWorkflow project
        var userRepo = DependencyResolver.Current.GetService&lt;IUserRepo&gt;(); //Throws exception :(
  </code></pre>
  
  <p>The exception is:
      The request lifetime scope cannot be created because the HttpContext is not available.</p>
  
  <p>Now before we start blaming the workflow, just know I had this exact set up working just fine in another solution the workflow was able to use DependencyResolver just fine. So I suspect this had to do with using a newer version of Autofac and the fact that the workflow runs asynchronously (just like the question i linked to regarding async code)</p>
  
  <p>I tried switching all the registration code to use <code>InstancePerLifetimeScope()</code> instead of <code>InstancePerHttpRequest()</code> and trying to create a scope:</p>
  
  <pre><code>using (var c= AutofacDependencyResolver.Current
                       .ApplicationContainer.BeginLifetimeScope("AutofacWebRequest"))
  {
     var userRepo = DependencyResolver.Current.GetServices&lt;IUserRepo&gt;();
  }
  </code></pre>
  
  <p>But it didnt change the exception. Breaking the code down even further here's the exact culprit:</p>
  
  <pre><code>var adr = AutofacDependencyResolver.Current; //Throws that exception 
  </code></pre>
  
  <p>Really need to get past this spent too much time stuck. Will reward existing answer with bounty in 2 days</p>