I'm using Autofac on an MVC project with Web API.
I'm registering all controllers:
builder.RegisterControllers(assembly);
builder.RegisterApiControllers(assembly);
and
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
I'm also using OWIN, and I'm registering a UserManager per OWIN request:
app.CreatePerOwinContext(() => AutofacDependencyResolver.Current.GetService<UserManager>());
The UserManager needs an instance of DbContext, which is configured to be instantiated once per request:
builder.RegisterType<DbContext>()
.AsSelf()
.InstancePerRequest();
Here is the problem:
When accessing a Web API controller, aDbContext is created to satisfy the CreatePerOwinContext call.
Then, in my controller, I inject another DbContext, and I expected the instance injected in the controller to be the same created in the CreatePerOwinContext call, but it is not!
If I access an MVC controller, everything works as expected.
I think Autofac is using two different resolvers, and not sharing instances between them. How can I solve this?