安装包: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的更多相关文章

  1. WebAPI2使用AutoFac依赖注入完整解决方案。

    WebApi2上进行依赖注入,在百度里能搜到的的完整解决方案的文章少之又少,缺胳膊断腿. 和MVC5依赖注入的不同之处,并且需要注意的地方,标记在注释当中.上Global代码: namespace S ...

  2. WebAPI2使用Autofac实现IOC属性注入完美解决方案

    一.前言 只要你是.NETer你一定IOC,IOC里面你也会一定知道Autofac,上次说了在MVC5实现属性注入,今天实现在WebApi2实现属性注入,顺便说一下autofac的程序集的注入方式,都 ...

  3. .NET领域最为流行的IOC框架之一Autofac WebAPI2使用Autofac实现IOC属性注入完美解决方案 AutoFac容器初步

    .NET领域最为流行的IOC框架之一Autofac   一.前言 Autofac是.NET领域最为流行的IOC框架之一,微软的Orchad开源程序使用的就是Autofac,Nopcommerce开源程 ...

  4. autofac.webapi2

    quick start https://autofaccn.readthedocs.io/en/latest/integration/webapi.html#quick-start To get Au ...

  5. 在MVC5和webAPI下是用Autofac依赖注入

    很多书本中都提到依赖注入,控制反转等概念,这些都是为了实现松耦合层.组件和类目的. 常见的是使用Repository类分离Controller和Model的直接联系.而为了解除Repository类和 ...

  6. 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 ...

  7. Asp.Net MVC 之 Autofac 初步使用3 集成web api

    今天我们试着在WebApi2实现autofac的注入,关于这方面也是看了几位园友的分享省了不少时间,所以结合着前篇的demo再新建webapi进行... 一样开篇还是发下大概demo结构: 还是nug ...

  8. webapi框架搭建-依赖注入之autofac

    前言 c#的依赖注入框架有unity.autofac,两个博主都用过,感觉unity比较简单而autofac的功能相对更丰富(自然也更复杂一点),本篇将基于前几篇已经创建好的webapi项目,引入au ...

  9. 使用Autofac动态注入启动Api服务

    Autofac Autofac(https://autofac.org/)是一款.NET的IOC组件,它可以和Owin, Web Api, ASP.NET MVC, .NET Core完美结合,帮助开 ...

随机推荐

  1. jquery操作表格总结

    返回表格行 或 列的索引 td是列,tr是行: <script type="text/javascript"> $(document).ready(function() ...

  2. javascript之ProtoBuf在websocket中的使用

    因为ProtoBuf的序列化效率和大小都非常好,所以它在网络通信上面应用越来越多:而webosocket也随着web3.0应用越来越广泛,而将这两个结合在一起的也会慢慢形成一种趋势:本人是为了测试自已 ...

  3. Hibernate基础知识总结

    Hibernate是JDBC的轻量级的对象封装(encapsulation),它是一个独立的对象持久persistence层框架. hibernate要做的事,就是让对象投影到关系数据库中,然后实施化 ...

  4. VB6之截图

    今天先把主要逻辑写出来,如果有时间就实现一个真正的截图工具. Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC ...

  5. ActiveMQ 503错误

    问题描述: 在Linux系统下安装ActiveMQ,启动服务 正常启动后,通过浏览器进行访问 可以正常显示home页面,但是点击其他菜单,如Queues,Topics等,都会出现503错误,如图 问题 ...

  6. linux 配置Apache 、PHP

    1. 安装 Apache 安装apache,首先要使用管理员权限,如果如法获取请联系您的管理员. centos: yum install httpd httpd-devel 2. 安装PHP 同样也需 ...

  7. Excel多表合并的宏

    Sub 合并当前目录下所有工作簿的全部工作表() Dim MyPath, MyName, AWbName Dim Wb As Workbook, WbN As String Dim G As Long ...

  8. 推荐给IT运维工程师必须学习的4本Linux书籍

    我们的人生如游戏,每个人都扮演着不同的角色,有普通玩家.NPC.普通野怪,终极Boss,都有不同的级别之分,我们在技术方面又何尝不是呢,我们大部分人都是普通野怪,遍地都是,很容易被别人虐,没有什么特殊 ...

  9. 数控G代码编程详解大全

    一.G代码功能简述 G00------快速定位 G01------直线插补 G02------顺时针方向圆弧插补 G03------逆时针方向圆弧插补 G04------定时暂停 G05------通 ...

  10. 快手 Android 工程师面经

    看着我把简历投完之后弹出的"完成"字样,我就十分的激动了,我是一名应届毕业生,老老实实的那种,学过的知识我都一步一个脚印的复习的完了,Lintcode上该刷的题,也妥妥的完成了,但 ...