Closed craigfowler closed 7 years ago
Discovered using v3.3.4
of the integration but still present in latest master at this point.
Nice catch, and thanks for the PR!
Has this fix also been applied to AutoFac.WebApi2? If not, can it be? I'm having the same issue, but in a WebApi project.
Web API works very differently than MVC when it comes to creating a request lifetime scope. If you're having a problem with Web API, please file an issue in that repository and include some details like exception call stack, link to a project where it's happening, etc. Thanks!
Using the Autofac MVC integration coexisting with the OWIN integration in the same application can result in an exception raised from the MVC integration's
RequestLifetimeScopeProvider
class.Reproduction
This can occur when an HTTP request uses only the OWIN middleware and does not make any use of the regular MVC5 stack. I experienced this when making use of OAuth functionality from OWIN.
I have only tried this with ASP.NET MVC5 and not the newer ASP.NET Core.
In my first reproduction case I was using an application which used both OWIN middleware (OAuth JWT bearer token auth in my case) which included some injected components and also ASP.NET MVC5 in the same app. I received the following exception when performing an HTTP request to the oauth JWT token endpoint:
Root cause
Because the MVC stack is not used,
HttpContext.Current
isnull
. However,RequestLifetimeHttpModule
triggersRequestLifetimeScopeProvider.EndLifetimeScope
regardless as it's registered as an HTTP module. The lifetime scope provider has a hard dependency uponHttpContext.Current
and crashes with an exception in this case.Workaround
You may work around this by subclassing
RequestLifetimeScopeProvider
as follows.Then, when configuring Autofac for MVC, construct and use the alternative provider explicitly: