本文转自:http://www.cnblogs.com/Hai--D/p/5992573.html

var builder = new ContainerBuilder();

// Mvc Register
builder.RegisterControllers(Assembly.GetExecutingAssembly()).AsSelf().PropertiesAutowired();
builder.RegisterFilterProvider();
builder.RegisterType<UserService>().As<IUserService>().InstancePerLifetimeScope(); //WebApi Register
builder.RegisterApiControllers(Assembly.GetExecutingAssembly()).AsSelf().PropertiesAutowired();
builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);
builder.RegisterWebApiModelBinderProvider(); var container = builder.Build(); // Set the dependency resolver for Web API.
var webApiResolver = new AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = webApiResolver; // Set the dependency resolver for MVC.
var resolver = new AutofacDependencyResolver(container);
DependencyResolver.SetResolver(resolver);

  

在实际Controller和ApiController中通过构造函数注入,这不必多说。

但是,在实际项目需求的时候,有些地方不方便使用构造函数,或者说就要使用服务定位IContainer.Resolve(ServiceType)的方式来获得服务的实例。

曾经在项目中看到过有人通过把Container设区全局静态变量来获得对象容器。这个方式在Local的情况下,不会有太大问题。在Mvc中,容器DependencyResolver.Current本身也是通过尽量变量来实现的。

 public class DependencyResolver
{
public DependencyResolver(); public static IDependencyResolver Current { get; }
...
}

  

但是和C端不同的是,Web服务是基于请求的,autofac内部的InstancePerLifetimeScope,InstancePerHttpRequest,InstancePerApiRequest等都是基于每次请求的Scope,而静态的Container明显生命周期不符合。

所以我们写代码的时候都是通过DependencyResolver.Current.GetService()和GlobalConfiguration.Configuration.DependencyResolver.GetService()来分别获取Mvc和WebApi的对象。那么问题来了,我有一段业务逻辑在BLL中,Mvc和WebApi可以都调用到,其中需要Resolve一个服务,那么如何来指定容器呢?

带着问题,我们先来看看DependencyResolver.Current和GlobalConfiguration.Configuration.DependencyResolver,通过一组实验来对比一下:

 public class WebApiApplication : System.Web.HttpApplication
{
public static System.Web.Mvc.IDependencyResolver mvcResolver;
public static System.Web.Http.Dependencies.IDependencyResolver apiResolver; protected void Application_Start()
{
...
       builder.RegisterType<UserService>().As<IUserService>().InstancePerLifetimeScope(); // Set the dependency resolver for Web API.
var webApiResolver = new AutofacWebApiDependencyResolver(container);
apiResolver = webApiResolver;
GlobalConfiguration.Configuration.DependencyResolver = webApiResolver; // Set the dependency resolver for MVC.
var resolver = new AutofacDependencyResolver(container);
mvcResolver = resolver;
DependencyResolver.SetResolver(mvcResolver);
}
}    public interface IUserService
{
} public class UserService : IUserService
{
}

  我们分别定义了两个静态变量mvcResolver和apiResolver来存储两个不同的容器,并注册了一组服务,指定其生命周期为InstancePerLifetimeScope,先看看Mvc的容器

 public class HomeController : Controller
{
IUserService _userservice; public HomeController(IUserService userService)
{
_userservice = userService;
var a = DependencyResolver.Current.GetService<IUserService>();
var b = WebApiApplication.mvcResolver.GetService<IUserService>();
var c1 = ReferenceEquals(userService, a); //true
var c2 = ReferenceEquals(userService, b); //true
var c3 = ReferenceEquals(b, a); //true
}
} public class ValuesController : ApiController
{
IUserService _userservice; public ValuesController(IUserService userService)
{
_userservice = userService;
var a = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(IUserService));
var b = WebApiApplication.apiResolver.GetService(typeof(IUserService));
var c1 = ReferenceEquals(userService, a); //false
var c2 = ReferenceEquals(userService, b); //false
var c3 = ReferenceEquals(b, a); //true
}
}

  

发现通过GlobalConfiguration.Configuration.DependencyResolver来获取的对象,竟然不等于构造函数解析出来的对象,有点毁三观。说明它并不是当前上下文的对象,也就是说这个对象的生命周期不在控制范围内。

那么Mvc和WebApi可不可以用同一个容器来指定呢?

我们先来看看stackoverflow上的这篇文章:Is it possible to configure Autofac to work with ASP.NET MVC and ASP.NET Web Api

其实Mvc和WebApi分别是两个独立的依赖解析器,这点没什么问题,一个是System.Web.Mvc.IDependencyResolver另一个是System.Web.Http.Dependencies.IDependencyResolver,两个互相不串。

最后,一个很重要的对象来了,那就是Autofac.IComponentContext,它就是解析的上下文,通过它来解析的对象是符合当前上下文的,我们再来看看之前的例子:

public class HomeController : Controller
{
IUserService _userservice; public HomeController(IUserService userService, IComponentContext com)
{
_userservice = userService;
var a = DependencyResolver.Current.GetService<IUserService>();
var b = WebApiApplication.mvcResolver.GetService<IUserService>();
var d = com.Resolve<IUserService>();
var d1 = ReferenceEquals(userService, d); //true
var c1 = ReferenceEquals(userService, a); //true
var c2 = ReferenceEquals(userService, b); //true
var c3 = ReferenceEquals(b, a); //true
}
}

  

//WebApi:
public class ValuesController : ApiController
{
IUserService _userservice; public ValuesController(IUserService userService, IComponentContext com)
{
_userservice = userService;
var a = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(IUserService));
var b = WebApiApplication.apiResolver.GetService(typeof(IUserService));
var d = com.Resolve<IUserService>();
var d1 = ReferenceEquals(userService, d); //true
var c1 = ReferenceEquals(userService, a); //false
var c2 = ReferenceEquals(userService, b); //false
var c3 = ReferenceEquals(b, a); //true
}
}

  

参考:

ASP .Net 4 Web Api RC + Autofac manual resolving

希望对你有帮助,如有错误,欢迎指出! 用属性自动注入需要是public,

如果,能支持 protected 注入,那就完美了

转:autofac在mvc和webapi集成的做法的更多相关文章

  1. autofac解析Mvc和Webapi的坑

    我们在项目中很早就开始使用autofac,也以为知道与mvc和webapi集成的做法. var builder = new ContainerBuilder(); // Mvc Register bu ...

  2. autofac + owin + webform + mvc + webapi集成demo

    http://git.oschina.net/shiningrise/AutofacOwinDemo using Microsoft.Owin; using Owin; using System.We ...

  3. Autofac 同时支持MVC 与Webapi

    1.引用 using Autofac; using Autofac.Integration.Mvc; using Autofac.Integration.WebApi; 2.在Global中的Appl ...

  4. AutoFac mvc和WebAPI 注册Service (接口和实现)

    AutoFac  mvc和WebAPI  注册Service (接口和实现) 1.准备组件版本:Autofac 3.5.0    Autofac.Integration.Mvc 3.3.0.0  (I ...

  5. 【半小时大话.net依赖注入】(下)详解AutoFac+实战Mvc、Api以及.NET Core的依赖注入

    系列目录 上|理论基础+实战控制台程序实现AutoFac注入 下|详解AutoFac+实战Mvc.Api以及.NET Core的依赖注入 前言 本来计划是五篇文章的,每章发个半小时随便翻翻就能懂,但是 ...

  6. AutoFac在MVC中的使用

    在asp.net mvc控制器中使用Autofac来解析依赖 如下Controller中使用构造函数依赖注入接口IUserService: public IUserService _IUserServ ...

  7. 记一次autofac+dapper+mvc的框架搭建实践

    1,环境 .net framework4.7.2,Autofac,Autofac.Mvc5,sql server 2,动机 公司项目用的是ef,之前留下代码的大哥,到处using,代码没有分层,连复用 ...

  8. 给Asp.Net MVC及WebApi添加路由优先级

    一.为什么需要路由优先级 大家都知道我们在Asp.Net MVC项目或WebApi项目中注册路由是没有优先级的,当项目比较大.或有多个区域.或多个Web项目.或采用插件式框架开发时,我们的路由注册很可 ...

  9. Autofac.Integration.Mvc分析

    Autofac.Integration.Mvc static ILifetimeScope LifetimeScope { get { return (ILifetimeScope)HttpConte ...

随机推荐

  1. NSCache类的简单介绍

    最近看SDWebImage,里面的内存缓存用到了NSCache这个类,由于以前没有使用过,特此记录学习一下. NSCache NSCache是苹果官方提供的缓存类,用法和NSMutableDicton ...

  2. python多进程理论

    什么是进程 进程:正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 举例(单核+多道,实现多个进程的并发执行): 你在一个时间段内有很多任务要做:python学习的任务,赚钱的任务,交女朋 ...

  3. PAT 1002. A+B for Polynomials (25)

    This time, you are supposed to find A+B where A and B are two polynomials. Input Each input file con ...

  4. git常用配置项

    1.默认的编辑器:core.editor git config --global core.editor emacs 2.默认提交模版:commit.template 假设你创建了一个叫 ~/.git ...

  5. Python常见序列详解

    一.Python中序列的分类 常见序列类型包括字符串(普通字符串和unicode字符串),列表和元组.所谓序列,即成员有序排列,可通过下标访问. 二.Python序列通用操作 下面我们将分别以字符串. ...

  6. Shallow Copy & Deep Copy in Python list

    今天在写一个小程序的时候用到了2维数组, 顺手就写成了[[0.0]*length]*length, 结果为了这个小错,调试了半个多小时, 其实之前对与浅复制和深复制已经做过学习和总结, 但真正编程用到 ...

  7. Way to Git

    最近在学习Git,我先后在CentOS6.4, Ubuntu12.04, Windows7上安装Git,遇到的问题比较多的是在CentOS上的安装,Ubuntu和Windows7上的安装相对比较简单, ...

  8. Django为什么要跳转到不同的页面来实现不同的功能

    其实是不同将信息提交给不同的页面交给不同的页面去处理同一个数据库,不同的模块实现不同的功能,当要实现某一个功能的时候直接跳转到那一个功能下面的url,可以把要实现的功能区分开,以python面向对象的 ...

  9. JPEGView——专业、免费、开源的图像浏览器

    虽叫JPEGView,它不仅支持jpeg图像格式,主流的图像格式它都支持. 试一试你就知道它有多强大了.

  10. Python编程-架构、Socket

    一.客户端/服务器架构 1.C/S架构 Client/Server架构,即服务器/客户端架构. 客户端和服务器端的程序不同,用户的程序主要在客户端,服务器端主要提供数据管理.数据共享.数据及系统维护和 ...