使用Autofac在ASP.NET Web API上实现依赖注入
在ASP.NET Web API里使用Autofac
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上实现依赖注入的更多相关文章
- ASP.NET Web API中的依赖注入
什么是依赖注入 依赖,就是一个对象需要的另一个对象,比如说,这是我们通常定义的一个用来处理数据访问的存储,让我们用一个例子来解释,首先,定义一个领域模型如下: namespace Pattern.DI ...
- 如何在 ASP.Net Web Forms 中使用依赖注入
依赖注入技术就是将一个对象注入到一个需要它的对象中,同时它也是控制反转的一种实现,显而易见,这样可以实现对象之间的解耦并且更方便测试和维护,依赖注入的原则早已经指出了,应用程序的高层模块不依赖于低层模 ...
- ASP.NET Web API上实现 Web Socket
1. 什么是Web Socket Web Socket是Html5中引入的通信机制,它为浏览器与后台服务器之间提供了基于TCP的全双工的通信通道.用以替代以往的LongPooling等comet st ...
- ASP.NET Web API上实现 Web Socket - 转
1. 什么是Web Socket Web Socket是Html5中引入的通信机制,它为浏览器与后台服务器之间提供了基于TCP的全双工的通信通道.用以替代以往的LongPooling等comet st ...
- 前端AngularJS后端ASP.NET Web API上传文件
本篇体验使用AngularJS向后端ASP.NET API控制器上传文件. 首先服务端: public class FilesController : ApiController { //usi ...
- ASP.NET Web API 上传文件
HTML表单: <form id="form1" method="post" enctype="multipart/form-data" ...
- ASP.NET Web API自身对CORS的支持: CORS授权检验的实施
通过<EnableCorsAttribute特性背后的故事>我们知道:由CorsPolicyProvider提供的CorsPolicy表示目标Action采用的资源授权策略,ASP.NET ...
- jQueryMobile 網頁使用 ASP.NET Web API 服務
微軟的 ASP.NET Web API 框架,能以 RESTful 輕量級的架構風格,建立 HTTP 服務,讓多種不同的用戶端,如: 手機.平板.電腦(PC),透過 HTTP 的 GET.POST.P ...
- [水煮 ASP.NET Web API2 方法论](1-1)在MVC 应用程序中添加 ASP.NET Web API
问题 怎么样将 Asp.Net Web Api 加入到现有的 Asp.Net MVC 项目中 解决方案 在 Visual Studio 2012 中就已经把 Asp.Net Web Api 自动地整合 ...
随机推荐
- 学号160809224姓名黄家帅c语言程序设计实验2 选择结构程序设计
实验2-1 输入3个数,并按由大到小的顺序输出. 实验要求: 编写一个C程序,输入3个数,并按由大到小的顺序输出. 源码: #include <stdio.h>void main(){ i ...
- linux kernel i2c底层代码跟踪
其实跟上次跟的平台总线有关 在arch/arm/mach-mx6/board-mx6q_sabresd.c 文件中 static void __init mx6_sabresd_board_init( ...
- nginx+nginx-rtmp-module+ffmpeg搭建流媒体服务器[转]
转 :http://redstarofsleep.iteye.com/blog/2123752 Nginx本身是一个非常出色的HTTP服务器,FFMPEG是非常好的音视频解决方案.这两个东西通过一个n ...
- MySQL Errcode 13 with SELECT INTO OUTFILE Can't create/write to file
这是由于权限问题导致的,最主要的问题是搞清楚权限是如何设置的.Ubuntu 使用 AppArmor 作为程序权限限制, Fedora 使用 selinux 作为程序权限限制. 在linux中,以往的权 ...
- window下安装mongodb及php mongo扩展
1.安装mongoDB 下载最新版本的 MongoDB 下载地址:http://www.mongodb.org/downloads 解压文件到 D:\mongodb 解压后的文件列表如下: 创建数据存 ...
- ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...
- Ubuntu下安装eclipse
一.eclipse安装环境JDK的安装 1.下载JDK 从官网下载jdk8 jdk-8u5-linux-x64.tar.gz 2.解压$ tar -zxvf jdk-8u5-linux-x64.tar ...
- ios The App Life Cycle
先推荐ios 必读文章 App Programming Guide for iOS ,请在苹果官网搜索,并仔细阅读所有内容 State Description Not running The app ...
- bbs/贴吧/盖楼的技术实现(PHP)
2015年3月5日 14:36:44 更新: 2015年7月18日 16:33:23 星期六 目标, 实现类似网易盖楼的功能, 但是不重复显示帖子 效果: * 回复 //1楼 ** 回复 //1楼的子 ...