How to plug my Autofac container into ASP. NET Identity 2.1

后端 未结 5 1226
攒了一身酷
攒了一身酷 2020-12-12 15:29

I have been looking into the new features of the new version of ASP.NET Identity 2.1 and one of its enhancements is the new IoC features integrated into the OWIN Middleware.

5条回答
  •  攒了一身酷
    2020-12-12 15:55

    Now detailed for MVC5 Owin integration on Autofac Docs:

    "

    • Do all the stuff for standard MVC integration - register controllers, set the dependency resolver, etc.
    • Set up your app with the base Autofac OWIN integration.
    • Add a reference to the Autofac.Mvc5.Owin NuGet package.
    • In your application startup class, register the Autofac MVC middleware after registering the base Autofac middleware.

      public class Startup
      {
        public void Configuration(IAppBuilder app)
        {
         var builder = new ContainerBuilder();
      
        // 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.
        var container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
      
        // OWIN MVC SETUP:
      
        // Register the Autofac middleware FIRST, then the Autofac MVC middleware.
        app.UseAutofacMiddleware(container);
        app.UseAutofacMvc();
        }
      }
      

      "

    I also have RoleManager wrapper so added:

    builder.RegisterType>().As>();
    

    as per SO answer

提交回复
热议问题