Hangfire job throws Autofac exception even though job class is not managed by Autofac

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

  1. Simplest, but hacky – set the private _dependencyResolver field of our ViewEngineCollection class instance through the reflection. Please google how to do it.
  2. Derive the ViewEngineCollection class and override its FindView(ControllerContext, string, string) method to not to use DependencyResolver.Current at all.
  3. Re-engineer Postal to not to use ViewEngineCollection class at all. As I understand, it is possible.