I'm playing around with ASP.NET vNext and I'd like to utilize InstancePerRequest
for some of my registrations, e.g MyDbContext
would be one example.
I've got Autofac
running using InstancePerLifetimeScope
for these dependencies. Which works....
But as soon as I change over to use InstancePerRequest
I get this common error:
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.
I've read the Autofac help docs around this.... but I'm not sure how to set up the "request lifetime scope" which is causing this exception.
Has anyone set up an ASP.NET 5 web app integrating Autofac including InstancePerRequest
?
HttpContext.Current
is null while resolving. What is the stack trace of the exception ? are you resolving a type outside of a HTTP request ? – Cyril Durand