安装包: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. abelkhan中的rpc框架

    rpc简介:http://www.ibm.com/developerworks/cn/aix/library/au-rpc_programming/index.html 常见的rpc框架有protob ...

  2. 【LeetCode】190. Reverse Bits

    题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...

  3. VMware中Mac OS中显示共享文件夹的方法

    在finder 偏好设置里的通用标签下,勾选  “已连接的服务器”

  4. 关于SpringMVC中text/plain的编码导致的乱码问题解决方法

    有老铁的项目出现个问题,就是用SpringMVC给前台返回一句话,是String类型的,然后前台接收到是乱码. 然后以为是简单的response的编码问题,就在方法体中开始给response设置编码, ...

  5. localStorage与location的用法

    1.localStorage 是h5提供的客户端存储数据的新方法: 之前,这些都是由 cookie 完成的.但是 cookie 不适合大量数据的存储,因为它们由每个对服务器的请求来传递,这使得 coo ...

  6. Java自学手记——多态

    对象转型 学习多态前先明白一个叫对象转型的概念,如: class Animal{ void sleep(){ System.out.println("睡觉"); } } class ...

  7. Spark Standalone Mode Configuration

    For currently popular distributed framework Spark, here is the intro and step to configure the spark ...

  8. voa 2015 / 4 / 13

    couch potato : someone who likes to spend a lot of time sitting or lying down while watching televis ...

  9. wifi扩展设置

    一.主路由器设置 网络参数 LAN口设置查到 MAC地址,用于设置扩展路由器 Bridge功能设置时 AP1的地址 2.无线基本设置,桥的 SSID BSSID 为扩展 3.无线安全设置 二.扩展路由 ...

  10. 在CI中实现持续Web安全扫描

    一. 当前Web应用安全现状 随着中国互联网金融的爆发和繁荣,Web应用在其中扮演的地位也越来越重要,比如Web支付系统.Web P2P系统.Web货币系统等.对于这些金融系统来讲,安全的重要性是不言 ...