Short Question: Same as this unanswered problem
Long Question:
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).
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.
Now I have 2 other projects in the new solution that need to pull a dependency. Lets just assume I have to use DependencyResolver.Current.GetService<T>() despite it being an anti-pattern.
At first this was not working until I set the MVC Dependency Resolver to the same container:
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));
The strange part is, doing that only fixed it in ONE of those projects! Here's the situation:
 Solution.Web project
      Bootstrapper.cs that registers both dependency resolvers for web api and mvc.
 Solution.ClassLib project
      var userRepo = DependencyResolver.Current.GetService<IUserRepo>(); //Good! :)
 Solution.WindowsWorkflow project
      var userRepo = DependencyResolver.Current.GetService<IUserRepo>(); //Throws exception :(
The exception is: The request lifetime scope cannot be created because the HttpContext is not available.
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)
I tried switching all the registration code to use InstancePerLifetimeScope() instead of InstancePerHttpRequest() and trying to create a scope:
using (var c= AutofacDependencyResolver.Current
                     .ApplicationContainer.BeginLifetimeScope("AutofacWebRequest"))
{
   var userRepo = DependencyResolver.Current.GetServices<IUserRepo>();
}
But it didnt change the exception. Breaking the code down even further here's the exact culprit:
var adr = AutofacDependencyResolver.Current; //Throws that exception 
Really need to get past this spent too much time stuck. Will reward existing answer with bounty in 2 days
                        
UPDATE Nov. 20, 2014: In releases of
Autofac.Mvc5since this question was released, the implementation ofAutofacDependencyResolver.Currenthas been updated to remove the need for anHttpContext. If you are encountering this problem and found this answer, you can potentially easily solve things by updating to a later version ofAutofac.Mvc5. However, I will leave the original answer intact for folks to understand why the original question asker was having issues.Original answer follows:
AutofacDependencyResolver.Currentrequires anHttpContext.Walking through the code,
AutofacDependencyResolver.Currentlooks like this:And, of course, if the current dependency resolver is an
AutofacDependencyResolverthen it's going to try to do a resolution...Which gets the lifetime scope from a
RequestLifetimeScopeProvider...It has to work like that to support tools like Glimpse that dynamically wrap/proxy the dependency resolver in order to instrument it. That's why you can't just cast
DependencyResolver.Current as AutofacDependencyResolver.Pretty much anything using the
Autofac.Integration.Mvc.AutofacDependencyResolverrequiresHttpContext.That's why you keep getting this error. It doesn't matter if you have no dependencies that are registered
InstancePerHttpRequest-AutofacDependencyResolverwill still require a web context.I'm guessing the other workflow app you had where this wasn't an issue was an MVC app or something where there was always a web context.
Here's what I'd recommend:
Autofac.Integration.WebApi.AutofacWebApiDependencyResolver.AutofacHostFactory.Containerand that host factory implementation to resolve dependencies. (WCF is a little weird with its singleton host potential, etc. so "per request" isn't quite as straightforward.)CommonServiceLocatorimplementation for Autofac. It doesn't create request lifetimes, but it may solve some problems.If you keep those things straight and don't try to use the various resolvers outside their native habitats, as it were, then you shouldn't run into issues.
You can fairly safely use
InstancePerApiRequestandInstancePerHttpRequestinterchangeably in service registrations. Both of these extensions use the same lifetime scope tag so the notion of an MVC web request and a web API request can be treated similarly even if the underlying lifetime scope in one case is based onHttpContextand the other is based onIDependencyScope. So you could hypothetically share a registration module across apps/app types and it should do the right thing.If you need the original Autofac container, store your own reference to it. Rather than assuming Autofac will return that container somehow, you may need to store a reference to your application container if you need to get it later for whatever reason.
That will save you a lot of trouble down the road.