Autofac in webapi2
安装包:Autofac.webapi2
注意:
install-package autofac.webapi2 (注意:您的项目中如果使用的是webapi2,此处必须为webapi2而不是webapi,否则在运行时将出现“重写成员“Autofac.Integration.WebApi.AutofacWebApiDependencyResolver.BeginScope()”时违反了继承安全性规则。重写方法的安全可访问性必须与所重写方法的安全可访问性匹配。”错误。)
在global 中 的Application_Start()添加
var builder = new ContainerBuilder();
//builder.RegisterControllers(Assembly.GetExecutingAssembly()); //Register MVC Controllers
builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); //Register WebApi Controllers
//Register any other components required by your code....
builder.RegisterType<UserTest2>().As<IUserTest>();
var container = builder.Build();
// DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); //Set the MVC DependencyResolver
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver((IContainer)container); //Set the WebApi DependencyResolver
UserTest接口:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace webapiTest
{
public interface IUserTest
{
int GetI(int i);
}
public class UserTest : IUserTest
{
public int GetI(int i)
{
return i;
}
}
public class UserTest2 : IUserTest
{
public int GetI(int i)
{
return i*i;
}
}
}
使用:
public class DefaultController : ApiController
{
private IUserTest _userTest;
public DefaultController(IUserTest userTest)
{
_userTest = userTest;
}
// GET: api/Default/5
public int Get(int id)
{
return _userTest.GetI(id);
}
}
参考:
http://autofac.readthedocs.io/en/latest/integration/webapi.html#quick-start
https://weblogs.asp.net/shijuvarghese/dependency-injection-in-asp-net-web-api-using-autofac
https://stackoverflow.com/questions/26358287/how-do-i-resolve-web-api-controllers-using-autofac-in-a-mixed-web-api-and-mvc-ap
https://github.com/autofac/Examples
扩展:container 需要管理起来的
http://www.cnblogs.com/niuww/p/5649632.html
Autofac in webapi2的更多相关文章
- WebAPI2使用AutoFac依赖注入完整解决方案。
WebApi2上进行依赖注入,在百度里能搜到的的完整解决方案的文章少之又少,缺胳膊断腿. 和MVC5依赖注入的不同之处,并且需要注意的地方,标记在注释当中.上Global代码: namespace S ...
- WebAPI2使用Autofac实现IOC属性注入完美解决方案
一.前言 只要你是.NETer你一定IOC,IOC里面你也会一定知道Autofac,上次说了在MVC5实现属性注入,今天实现在WebApi2实现属性注入,顺便说一下autofac的程序集的注入方式,都 ...
- .NET领域最为流行的IOC框架之一Autofac WebAPI2使用Autofac实现IOC属性注入完美解决方案 AutoFac容器初步
.NET领域最为流行的IOC框架之一Autofac 一.前言 Autofac是.NET领域最为流行的IOC框架之一,微软的Orchad开源程序使用的就是Autofac,Nopcommerce开源程 ...
- autofac.webapi2
quick start https://autofaccn.readthedocs.io/en/latest/integration/webapi.html#quick-start To get Au ...
- 在MVC5和webAPI下是用Autofac依赖注入
很多书本中都提到依赖注入,控制反转等概念,这些都是为了实现松耦合层.组件和类目的. 常见的是使用Repository类分离Controller和Model的直接联系.而为了解除Repository类和 ...
- 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 in ...
- Asp.Net MVC 之 Autofac 初步使用3 集成web api
今天我们试着在WebApi2实现autofac的注入,关于这方面也是看了几位园友的分享省了不少时间,所以结合着前篇的demo再新建webapi进行... 一样开篇还是发下大概demo结构: 还是nug ...
- webapi框架搭建-依赖注入之autofac
前言 c#的依赖注入框架有unity.autofac,两个博主都用过,感觉unity比较简单而autofac的功能相对更丰富(自然也更复杂一点),本篇将基于前几篇已经创建好的webapi项目,引入au ...
- 使用Autofac动态注入启动Api服务
Autofac Autofac(https://autofac.org/)是一款.NET的IOC组件,它可以和Owin, Web Api, ASP.NET MVC, .NET Core完美结合,帮助开 ...
随机推荐
- 自己动手写一个自动登录脚本gg
1.下载一个sshpass工具 2.安装sshpass,安装到tools文件夹 3.把tools文件夹的路径加入到/etc/bashrc vim /etc/bashrc 最后一行 : expor ...
- Entity Framework入门教程:创建实体数据模型
下图为一个已经创建好的数据库表关系 实体数据模型的创建过程 在Visual Studio项目中,右键程序集菜单,选择[添加]->[新建项],在[添加新项窗口]中选择[ADO.NET实体数据模型] ...
- http请求的完整过程
HTTP通信机制是在一次完整的HTTP通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤: 1. 建立TCP连接 在HTTP工作开始之前,Web浏览器首先要通过网络与Web服务器建立连接,该 ...
- 关于SpringMVC中text/plain的编码导致的乱码问题解决方法
有老铁的项目出现个问题,就是用SpringMVC给前台返回一句话,是String类型的,然后前台接收到是乱码. 然后以为是简单的response的编码问题,就在方法体中开始给response设置编码, ...
- [图形学] 计算机图形学 with OpenGL开篇
<计算机图形学>(第四版)正在学习中,学习目的是为了在Unity中使用shader实现不同的渲染效果. 希望在这里能把学习过程中学到的知识和遇到的问题记录下来. 工作环境是:Xcode 8 ...
- 基于JAX-WS的WebService实现
JAX-WS是一套Java用于开发XML Web Services的技术规范,它的实现一般有CXF.AXIS和JDK(version>=1.6),借助这些我们可以进行SOAP服务开发. CXF和 ...
- Java 异常处理笔记
Java程序运行过程中所发生的异常事件可分为两类: §错误(Error):JVM系统内部错误.资源耗尽等严重情况 §违例(Exception): 其它因编程错误或偶然的外在因素导致的一般性问题,例如: ...
- 如何删除 SQL Server 表中的重复行
第一种:有主键的重复行,就是说主键不重复,但是记录的内容重复比如人员表tab ,主键列id,身份证编号idcard当身份证重复的时候,保留最小id值的记录,其他删除delete a from tab ...
- 用户代理字符串(navigator.userAgent)检测方法
最近在看<JavaScript 高级程序设计(第三版)>,发现其中关于用户代理字符串检测技术的一些方法,觉得讲的很详细.用户代理字符串(navigator.userAgent)中包含了大量 ...
- 【Vue】Vue的依赖追踪系统 ——搞懂methods watch和compute
从作用机制和性质上看待methods,watch和computed的关系 <他三个是啥子关系呢?> 首先要说,methods,watch和computed都是以函数为基础的,但各自却都不同 ...