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

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

时间: 2024-10-22 20:10:12

Autofac Mvc的相关文章

Autofac - MVC/WebApi中的应用

参考页面: http://www.yuanjiaocheng.net/ASPNET-CORE/asp-net-core-overview.html http://www.yuanjiaocheng.net/ASPNET-CORE/asp.net-core-environment.html http://www.yuanjiaocheng.net/ASPNET-CORE/newproject.html http://www.yuanjiaocheng.net/webapi/web-api-gais

AutoFac+MVC+WebApi源码----我踩过的坑

发现网上关于AutoFac的Demo源码比较少,综合MVC和WepApi的更少.所以贴出源码 WebApi项目(MVC4不需要引用,历史遗留问题,人懒没删) 建项目 新建类库IAutoFacDal(接口),AutoFacDal,IAutoFacLogic(接口),AutoFacLogic,AutoFacDto(参数),AutoFacEntity(数据库实体) 新建WebApi AutoFacEntity User源码 1 using System; 2 using System.Collecti

解决Autofac MVC 自动注入在 Areas拆分到不同dll下的注入失败问题走奏谞踪昨兹

position:static(静态定位) 当position属性定义为static时,可以将元素定义为静态位置,所谓静态位置就是各个元素在HTML文档流中应有的位置 podisition定位问题.所以当没有定义position属性时,并不说明该元素没有自己的位置,它会遵循默认显示为静态位置,在静态定位状态下无法通过坐标值(top,left,right,bottom)来改变它的位置. position:absolute(绝对定位) 当position属性定义为absolute时,元素会脱离文档流

OWIN support for the Web API 2 and MVC 5 integrations in Autofac

Currently, in the both the Web API and MVC frameworks, dependency injection support does not come into play until after the OWIN pipeline has started executing. This is simply a result of the OWIN support being added to both frameworks after their in

ASP.NET MVC 5 使用autofac实现DI

使用Nuget添加Autofac.MVC的引用 启动项设置 注册Controller 注册ModelBinder 注册相关的web abstraction 为View层启用属性注入 为Action Filter启用属性注入 使用Nuget添加Autofac.MVC的引用 启动项设置 protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RouteConfig.RegisterRoutes(RouteTa

AutoFac (控制反转IOC 与依赖注入DI)

绝世好文  转:http://blog.csdn.net/fanbin168/article/details/51293218 1 IOC概念(很重要) 项目 先引入AutoFac 和AutoFac MVC两个程序集到项目中 然后我们在MVC(UI层)的App_Start文件夹下创建一个AutoFacConfig.cs类 [csharp] view plain copy using System; using System.Collections.Generic; using System.Li

MVC学习

MVC 全新的membership框架Asp.net Identity(1)——.Net membership的历史 2014-12-30 08:00 by JustRun, 1532 阅读, 收藏, 编辑 在Asp.net上,微软的membershop框架经历了Asp.net membership到Asp.net simple membership,再到现在的Asp.net Identity. 每一次改变,都使得验证框架更加的适应变化和可定制.这篇文章是Asp.net Identity系列的开

新做的NHibernate项目,大家来拍拍砖,同时还要请多多提些问题

简单说明: 这个项目的结构,大家来拍拍砖,同时还要请多多提些问题. 项目不是为了做新闻,由于对新闻比较了解,不同的项目中也用的多,我就想通过这个项目吧新闻功能做的深入完善些,这里也就用新闻做演示用. 这个结构是根据之前的项目结构,以及院子里不是朋友们的提供的方案.代码等,整合起来的.不敢妄称为啥框架,只是希望对开发的项目能起到一些帮助作用. 第一次写blog,可能有点乱,大家见谅了. 使用结构资源 NHibernate.netFluentNHibernateAutofacMVCJquery 项目

AutoFac Asp.Net Mvc

autofac依赖注入,帮我们实例化接口,无需使用传统的New 如: public class AutoFacController : Controller { public IPeople _people; public AutoFacController(IPeople people) { _people = people; } // GET: AutoFac public ActionResult Index() { ViewBag.test = _people.Getpeople();