You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In your application startup class, register the Autofac MVC middleware after registering the base Autofac middleware.
publicclassStartup{publicvoidConfiguration(IAppBuilderapp){varbuilder=newContainerBuilder();// STANDARD MVC SETUP:// Register your MVC controllers.builder.RegisterControllers(typeof(MvcApplication).Assembly);// Run other optional steps, like registering model binders,// web abstractions, etc., then set the dependency resolver// to be Autofac.varcontainer=builder.Build();DependencyResolver.SetResolver(newAutofacDependencyResolver(container));// OWIN MVC SETUP:// Register the Autofac middleware FIRST, then the Autofac MVC middleware.app.UseAutofacMiddleware(container);app.UseAutofacMvc();}}
Minor gotcha: MVC doesn't run 100% in the OWIN pipeline. It still needs HttpContext.Current and some other non-OWIN things. At application startup, when MVC registers routes, it instantiates an IControllerFactory that ends up creating two request lifetime scopes. It only happens during app startup at route registration time, not once requests start getting handled, but it's something to be aware of. This is an artifact of the two pipelines being mangled together.