Good, thank you for the detailed response. Looks like the problem is non-trivial for Autofac and MVC implementation.
The Cause
We are using MVC’s ViewEngineCollection
class to help Postal’s EmailService
class to render the view:
var engines = new ViewEngineCollection();
The ViewEngineCollection
class implicitly uses MVC’s DependencyResolver.Current
, that is set to Autofac’s AutofacDependencyResolver
class as we can see from the stack trace. And since the latter class uses RequestLifetimeScope
class to obtain registered services, it requires HttpContext.Current
to be available every time we try to resolve a dependency.
As we can see from the ViewEngineCollection
source code, there is a constructor overload to pass custom DependencyResolver
instance to the ViewEngineCollection
instance, but it is an internal one. So, the simplest and obvious solution – pass another dependency resolver – is unavailable.
Possible solutions
- Simplest, but hacky – set the private
_dependencyResolver
field of ourViewEngineCollection
class instance through the reflection. Please google how to do it. - Derive the
ViewEngineCollection
class and override itsFindView(ControllerContext, string, string)
method to not to useDependencyResolver.Current
at all. - Re-engineer
Postal
to not to useViewEngineCollection
class at all. As I understand, it is possible.