使用Autofac在ASP.NET Web API上实现依赖注入
2014-10-28 18:11
946 查看
在ASP.NET Web API里使用Autofac
1.通过NuGet安装Autofac.WebApi(当时安装的是Autofac 3.1.0)
PM > Install-Package Autofac.WebApi
2.在App_Start文件夹下新建AutofacWebApiConfig类
3.在Global中添加以下代码
1.通过NuGet安装Autofac.WebApi(当时安装的是Autofac 3.1.0)
PM > Install-Package Autofac.WebApi
2.在App_Start文件夹下新建AutofacWebApiConfig类
public class AutofacWebApiConfig { public static void Run() { SetAutofacWebApi(); } private static void SetAutofacWebApi() { ContainerBuilder builder = new ContainerBuilder(); HttpConfiguration config = GlobalConfiguration.Configuration; // Register API controllers using assembly scanning. builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); builder.RegisterType<ValueService>().As<IValueService>() .InstancePerApiRequest(); var container = builder.Build(); // Set the WebApi dependency resolver. config.DependencyResolver = new AutofacWebApiDependencyResolver(container); }
3.在Global中添加以下代码
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); //Call Autofac DI configurations AutofacWebApiConfig.Run(); }
相关文章推荐
- 使用Autofac在ASP.NET Web API上实现依赖注入
- 使用Autofac在ASP.NET Web API上实现依赖注入
- 使用Autofac在ASP.NET Web API上实现依赖注入
- 使用Autofac在ASP.NET Web API上实现依赖注入
- 使用AutoFac在ASP.NET Web API上实现依赖注入
- 使用Unity 实现ASP.NET Web API 依赖注入
- Asp.Net Mvc使用Autofac实现依赖注入
- ADO.NET .net core2.0添加json文件并转化成类注入控制器使用 简单了解 iTextSharp实现HTML to PDF ASP.NET MVC 中 Autofac依赖注入DI 控制反转IOC 了解一下 C# AutoMapper 了解一下
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【四】——实现模型工厂,依赖注入以及格式配置
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【四】——实现模型工厂,依赖注入以及格式配置
- ASP.NET Core2使用Autofac实现IOC依赖注入竟然能如此的优雅简便
- 扩展ASP.NET MVC三层框架并使用StructureMap实现依赖注入2-Repository层的实现
- 扩展ASP.NET MVC三层框架并使用StructureMap实现依赖注入1
- 扩展ASP.NET MVC三层框架且使用StructureMap实现依赖注入1-Model层
- Asp.net MVC 中使用 Ninject 实现依赖注入
- ASP.NET Core依赖注入解读&使用Autofac替代实现
- ASP.NET Core中使用IOC三部曲(二.采用Autofac来替换IOC容器,并实现属性注入)
- ASP.NET Core依赖注入解读&使用Autofac替代实现
- ASP.NET Web API实践系列07,获取数据, 使用Ninject实现依赖倒置,使用Knockout实现页面元素和视图模型的双向绑定
- [ASP.NET MVC 小牛之路]05 - 使用 Ninject实现依赖注入