zoukankan      html  css  js  c++  java
  • Autofac Mvc

    MVC

    Autofac is always kept up to date to support the latest version of ASP.NET MVC, so documentation is also kept up with the latest. Generally speaking, the integration remains fairly consistent across versions.

    MVC integration requires the Autofac.Mvc5 NuGet package.

    MVC integration provides dependency injection integration for controllers, model binders, action filters, and views. It also adds per-request lifetime support.

    This page explains ASP.NET classic MVC integration. If you are using ASP.NET Core, see the ASP.NET Core integration page.

    Quick Start

    To get Autofac integrated with MVC you need to reference the MVC integration NuGet package, register your controllers, and set the dependency resolver. You can optionally enable other features as well.

    protected void Application_Start()
    {
      var builder = new ContainerBuilder();
    
      // Register your MVC controllers. (MvcApplication is the name of
      // the class in Global.asax.)
      builder.RegisterControllers(typeof(MvcApplication).Assembly);
    
      // OPTIONAL: Register model binders that require DI.
      builder.RegisterModelBinders(typeof(MvcApplication).Assembly);
      builder.RegisterModelBinderProvider();
    
      // OPTIONAL: Register web abstractions like HttpContextBase.
      builder.RegisterModule<AutofacWebTypesModule>();
    
      // OPTIONAL: Enable property injection in view pages.
      builder.RegisterSource(new ViewRegistrationSource());
    
      // OPTIONAL: Enable property injection into action filters.
      builder.RegisterFilterProvider();
    
      // OPTIONAL: Enable action method parameter injection (RARE).
      builder.InjectActionInvoker();
    
      // Set the dependency resolver to be Autofac.
      var container = builder.Build();
      DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    }
    

    The sections below go into further detail about what each of these features do and how to use them.

    Register Controllers

    At application startup, while building your Autofac container, you should register your MVC controllers and their dependencies. This typically happens in an OWIN startup class or in the Application_Start method in Global.asax.

    var builder = new ContainerBuilder();
    
    // You can register controllers all at once using assembly scanning...
    builder.RegisterControllers(typeof(MvcApplication).Assembly);
    
    // ...or you can register individual controlllers manually.
    builder.RegisterType<HomeController>().InstancePerRequest();
    

    Note that ASP.NET MVC requests controllers by their concrete types, so registering them As<IController>() is incorrect. Also, if you register controllers manually and choose to specify lifetimes, you must register them as InstancePerDependency() or InstancePerRequest() - ASP.NET MVC will throw an exception if you try to reuse a controller instance for multiple requests.

    Set the Dependency Resolver

    After building your container pass it into a new instance of the AutofacDependencyResolver class. Use the static DependencyResolver.SetResolver method to let ASP.NET MVC know that it should locate services using the AutofacDependencyResolver. This is Autofac’s implementation of the IDependencyResolver interface.

    var container = builder.Build();
    DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    
  • 相关阅读:
    SDN课程阅读作业(2)
    2019 SDN上机第5次作业
    linux 最常用命令
    tomcat 部署 React 项目后,浏览器刷新报404问题
    Java8 ~ 特性
    React ~ 小结
    ES6 map与filter
    Idea 快捷键
    实现div可以调整高度(div实现resize)
    linux常用命令(4)
  • 原文地址:https://www.cnblogs.com/chucklu/p/12614130.html
Copyright © 2011-2022 走看看