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
- Register Controllers
- Set the Dependency Resolver
- Register Model Binders
- Register Web Abstractions
- Enable Property Injection for View Pages
- Enable Property Injection for Action Filters
- Enable Injection of Action Parameters
- OWIN Integration
- Using “Plugin” Assemblies
- Using the Current Autofac DependencyResolver
- Glimpse Integration
- Unit Testing
- Example
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));
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2019-04-01 How do I check if a type is a subtype OR the type of an object?
2017-04-01 What are the differences between WebAPI and WebAPI 2
2016-04-01 SuperSocketClientEngine
2015-04-01 .NET_Framework_version_history
2015-04-01 Tuple元祖