使用Autofac在ASP.NET Web API上实现依赖注入
【原文】 Dependency Injection in ASP.NET Web API using Autofac
摘要
在ASP.NET Web API里使用Autofac
通过NuGet安装Autofac.WebApi。(当然要先安装Autofac.dll)。
PM > Install-Package Autofac.WebApi
引用如下命名空间。
using Autofac;
using Autofac.Integration.WebApi;
再按照如下代码配置Autofac。
public static class Bootstrapper
{
public static void Run()
{
SetAutofacWebAPI();
}
private static void SetAutofacWebAPI()
{
var configuration = GlobalConfiguration.Configuration;
var builder = new ContainerBuilder();
// Configure the container
builder.ConfigureWebApi(configuration);
// Register API controllers using assembly scanning.
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterType<DefaultCommandBus>().As<ICommandBus>()
.InstancePerApiRequest();
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>()
.InstancePerApiRequest();
builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>()
.InstancePerApiRequest();
builder.RegisterAssemblyTypes( typeof (CategoryRepository)
.Assembly).Where(t => t.Name.EndsWith( " Repository " ))
.AsImplementedInterfaces().InstancePerApiRequest();
var services = Assembly.Load( " EFMVC.Domain " );
builder.RegisterAssemblyTypes(services)
.AsClosedTypesOf( typeof (ICommandHandler<>))
.InstancePerApiRequest();
builder.RegisterAssemblyTypes(services)
.AsClosedTypesOf( typeof (IValidationHandler<>))
.InstancePerApiRequest();
var container = builder.Build();
// Set the WebApi dependency resolver.
var resolver = new AutofacWebApiDependencyResolver(container);
configuration.ServiceResolver.SetResolver(resolver);
}
}
在Application_Start里调用Bootstrapper.Run()。
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
BundleTable.Bundles.RegisterTemplateBundles();
// Call Autofac DI configurations
Bootstrapper.Run();
}
Autofac.Mvc4
Autofac ASP.NET MVC integration已经升级到MVC4。 NuGet pacakge Autofac.Mvc4 。它提供了ASP.NET MVC4里的依赖注入(不包括Web API)。 Autofac.Mvc3 与 Autofac.Mvc4 没有什么语法上的不同。
源码
http://efmvc.codeplex.com/
一个样例Web程序,用来展示ASP.NET MVC、EF Code First以及架构实践。
相关资源:
Autofac官网 http://code.google.com/p/autofac/
Autofac ASP.NET MVC3 Ingetation http://code.google.com/p/autofac/wiki/Mvc3Integration
分类: ASP.NET MVC , Autofac
使用Autofac在ASP.NET Web API上实现依赖注入
摘要: 【原文】Dependency Injection in ASP.NET Web API using Autofac摘要在ASP.NET Web API里使用Autofac通过NuGet安装Autofac.WebApi。(当然要先安装Autofac.dll)。PM > Install-Package Autofac.WebApi引用如下命名空间。using Autofac;using Autofac.Integration.WebApi;再按照如下代码配置Autofac。 public static class Bootstrapper { public static voi... 阅读全文
posted @ 2012-04-03 11:22 阿福 阅读(571) | 评论 (0) 编辑
ASP.NET MVC 4, ASP.NET Web API, ASP.NET Web Pages v2 (Razor)全部开源,并接受来自社区的贡献(contributions)
摘要: 更新:看来微软的一小步引起了社区的大反响,看看来自Phil Haack的评论(前微软ASP.NET MVC Project Manager现已加入GitHub)。看来微软走出这一步还是不容易的,(不是指open souce,而是指accept external contributions)。来自Scott Guthrie以及Scott Hanselman博客的消息,ASP.NET MVC 4, ASP.NET Web API, ASP.NET Web Pages v2 (Razor)全部开源,并接受来自社区的贡献(contributions)。源码仍通过CodePlex发布,地址是http:/ 阅读全文
posted @ 2012-03-28 13:21 阿福 阅读(1303) | 评论 (3) 编辑
作者: Leo_wl
出处: http://www.cnblogs.com/Leo_wl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息查看更多关于使用Autofac在ASP.NET Web API上实现依赖注入的详细内容...