码迷,mamicode.com
首页 > Web开发 > 详细

Autofac Mvc

时间:2020-04-01 17:55:57      阅读:51      评论:0      收藏:0      [点我收藏+]

标签:using   nuget   dual   TBase   rtu   top   document   testing   span   

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));

 

 

 

 

 

 

 

 

 

 

 

 

 

Autofac Mvc

标签:using   nuget   dual   TBase   rtu   top   document   testing   span   

原文地址:https://www.cnblogs.com/chucklu/p/12614130.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!