安装包: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. Educational Codeforces Round 22.B 暴力

    B. The Golden Age time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  2. php---tp框架---表单验证

    自动验证是ThinkPHP模型层提供的一种数据验证方法,可以在使用create创建数据对象的时候自动进行数据验证.分为静态验证和动态验证. 关于基础知识,请查看手册"自动验证"一章 ...

  3. Eclipse 快捷键和模板设置

    快捷键设置 菜单  Window --> Preferences---General---Keys Content Assist:  代码提示快捷键 模板设置 新建一个模板 在Insert Va ...

  4. C# 哈希表(Hashtable)用法笔记

    一.什么是Hashtable? Hashtable 类代表了一系列基于键的哈希代码组织起来的键/值对.它使用键来访问集合中的元素. 当您使用键访问元素时,则使用哈希表,而且您可以识别一个有用的键值.哈 ...

  5. C实现dos图文菜单程序实例

      前言 公司一台服务器是novell环境,文件管理是基于dos6.22的,客户端启动需要一个图文菜单.   实现    编程环境:汉化版TC2.0 菜单基本功能:显示提示项.显示dbf中的行情信息. ...

  6. POJ 3659 Cell Phone Network / HUST 1036 Cell Phone Network(最小支配集,树型动态规划,贪心)-动态规划做法

    POJ 3659 Cell Phone Network / HUST 1036 Cell Phone Network(最小支配集,树型动态规划,贪心) Description Farmer John ...

  7. Andorid源码系列:View的onTouchEvent()与performClick(),performLongClick()调用时机解析

    这是大土豆的第一篇博客,想着工作3年多了,在工作上从一名菜鸟逐渐成长为在项目中能干点事的人,自己对Android的见解也一步步加深,有必要写一些对Android代码和开发过程中的感悟,和广大朋友们分享 ...

  8. 阿里云VPC绑定EIP实现SNAT

    阿里云VPC需要了解的几个问题 什么是VPC 虚拟私有网络(Virtual Private Network),能够帮助用户基于阿里云构建出一个隔离的网络环境.用户可以完全掌控自己的虚拟网络,包括选择自 ...

  9. 加减号改变input[type=number]的数值,基于[zepto.js]

    通过点击加减号可以更改input的数值,样式如下图: 具体的html代码如下: <div class="xh-lxx-cart-count1"> <span cl ...

  10. laravel数据库查询返回的数据形式

    版本:laravel5.4+ 问题描述:laravel数据库查询返回的数据不是单纯的数组形式,而是数组与类似stdClass Object这种对象的结合体,即使在查询构造器中调用了toArray(), ...