ASPNET5 依赖注入(Dependency Injection)
- 应用程序级别:通过HttpContext.ApplicationServices 属性提供给 MiddleWare
- 请求级别:通过HttpContext.RequestServices属性提供给MiddleWare,这个范围的ServiceProvider通过一个隐式的MIddleWare在最开始的请求管理道中被创建,在请求最后返回响应前被释放。
- IServiceProvider
- Dispose()
- Singleton 单个实例在整个应用程序中
- Scoped 单个实例在范围窗口内
- ServiceType 用来替换具体实现类的接口,Type类型
- ImplementationType 上面这个接口的具体实现类型,Type类型
- Lifetime 服务的生命周期,Singleon,Scoped 或者 Transient
- ImplementaionFactory 类型Func<IServiceProvider,Object>, 在一些场景中,开发人员希望提供一个工厂方法去创建具体实现类。他们之间是互相独立的,如果你提供了ImplementationType,那不能再提供ImpletentaionFactory.
- ImplementationInstance 具体事例
publicvoidConfigureServices(IServiceCollection services)
{
var serviceDescriptor = newServiceDescriptor(typeof(IBankManager), typeof(BankManager), ServiceLifetime.Transient);
services.Add(serviceDescriptor); // Add MVC services to the services container.
services.AddMvc();
}
备注: 创建serviceDescriptor有一些繁琐, 这就是你看到一些中间件用扩展方法去创建ServiceDescriptors, 像“service.AddMvc()”
- Hosting engine 会创建一个IServiceCollection,是ServiceDescriptor的集合对象
- Hosting engine 会添加全部它所需要的服务
- Hosting engine 会确认在程序集中有个Startup类,并有个ConfigureServices方法
- Hosting engine 会去加载这个方法,并传递IServiceCollection
- Startup类中的ConfigureSerivces会添加应用程序需要的服务至集合中
- Hosting engine 接着会创建 DefaultServiceProvider(ICO容器),并注册服务集合中IServiceCollection中的服务
- Hosting engine 会创建一个应用程序构建器(IApplicationBuilder)并给IApplicationBuilder.ApplicationServices分配一,个新的Service Provider,并进一步的使用它
- Hosting engin 在Startup.Configuer执行前会创建一个RequestServicesContainerMiddleware中间件,我会在稍后介绍它
- Hosting engine 会执行Startup类中 Configure方法,并传递Application Builder去创建中间件。如果需要Service Provider可通过ApplicationServices属性去创建中间件。
当第一个请求过来时,Httpcontext会被创建并传给第一个Middleware中的Invokde方法中,并会一直传到整个Middleware.但是在处理第一个中间件之前,Application Builder 's Service Provider 会被分配到HttpContext.ApplicationServices,确保整个中间件都可以通过这个属性获得需要的服务,不过,你应该记住这是一个应用程序级别的服务提供者,并且依赖于你选择的Ioc容器,如果你使用了它,你的对象可能会一直驻留在应用程序整个生活周期中.
好吧,这是一个应用程序级别的服务提供者,那有没有每次请求范围里的的服务提供者?
public async Task Invoke(HttpContext httpContext)
{
using(varcontainer = RequestServicesContainer.EnsureRequestServices(httpContext, _services))
{
await_next.Invoke(httpContext);
}
}
- 请求正在被RequestServiceContainerMiddleware处理
- Invoke方法会通过application-level Service Provider创建一个IServiceScopeFactory.
- IServiceScopeFactory会创建一个范围容器(scoped container)
- scoped container会被指派给属性HttpContext.RequestServices
- Invoke方法调用接下来的Middleware
- 当全部的Middleware都被执行完后,并返回到了RequestServiceContainerMiddleware,Scoped Container会被销毁通过"Using"
private async Task InvokeActionAsync(RouteContext context, ActionDescriptor actionDescriptor)
{
var services = context.HttpContext.RequestServices;
Debug.Assert(services != null); var actionContext = new ActionContext(context.HttpContext, context.RouteData, actionDescriptor); var optionsAccessor = services.GetRequiredService<IOptions<MvcOptions>>();
actionContext.ModelState.MaxAllowedErrors = optionsAccessor.Options.MaxModelValidationErrors; var contextAccessor = services.GetRequiredService<IScopedInstance<ActionContext>>();
contextAccessor.Value = actionContext;
var invokerFactory = services.GetRequiredService<IActionInvokerFactory>();
var invoker = invokerFactory.CreateInvoker(actionContext);
if (invoker == null)
{
LogActionSelection(actionSelected: true, actionInvoked: false, handled: context.IsHandled); throw new InvalidOperationException(
Resources.FormatActionInvokerFactory_CouldNotCreateInvoker(
actionDescriptor.DisplayName));
} await invoker.InvokeAsync();
}
ASPNET5 依赖注入(Dependency Injection)的更多相关文章
- 控制反转Inversion of Control (IoC) 与 依赖注入Dependency Injection (DI)
控制反转和依赖注入 控制反转和依赖注入是两个密不可分的方法用来分离你应用程序中的依赖性.控制反转Inversion of Control (IoC) 意味着一个对象不会新创建一个对象并依赖着它来完成工 ...
- 简明依赖注入(Dependency Injection)
前言 这是因特奈特上面不知道第几万篇讲依赖注入(Dependency Injection)的文章,但是说明白的却寥寥无几,这篇文章尝试控制字数同时不做大多数. 首先,依赖注入的是一件很简单的事情. 为 ...
- 14.AutoMapper 之依赖注入(Dependency Injection)
https://www.jianshu.com/p/f66447282780 依赖注入(Dependency Injection) AutoMapper支持使用静态服务定位构建自定义值解析器和自定 ...
- 依赖注入 | Dependency Injection
原文链接: Angular Dependency Injection翻译人员: 铁锚翻译时间: 2014年02月10日说明: 译者认为,本文中所有名词性的"依赖" 都可以理解为 & ...
- Spring点滴七:Spring中依赖注入(Dependency Injection:DI)
Spring机制中主要有两种依赖注入:Constructor-based Dependency Injection(基于构造方法依赖注入) 和 Setter-based Dependency Inje ...
- 设计模式之————依赖注入(Dependency Injection)与控制反转(Inversion of Controller)
参考链接: 依赖注入(DI) or 控制反转(IoC) laravel 学习笔记 —— 神奇的服务容器 PHP 依赖注入,从此不再考虑加载顺序 名词解释 IoC(Inversion of Contro ...
- 理解依赖注入(Dependency Injection)
理解依赖注入 Yii2.0 使用了依赖注入的思想.正是使用这种模式,使得Yii2异常灵活和强大.千万不要以为这是很玄乎的东西,看完下面的两个例子就懂了. class SessionStorage { ...
- AngularJS - 依赖注入(Dependency Injection)
点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ 依赖注入 依赖注入是软件设计模式中的一部分,用于处理组件是如何得到它说依赖的其它组件的. ...
- Spring之对象依赖关系(依赖注入Dependency Injection)
承接上篇: Spring中,如何给对象的属性赋值: 1:通过构造函数,如下所示: <!-- 1:构造函数赋初始值 --><bean id="user1" clas ...
- MVC使用StructureMap实现依赖注入Dependency Injection
使用StructureMap也可以实现在MVC中的依赖注入,为此,我们不仅要使用StructureMap注册各种接口及其实现,还需要自定义控制器工厂,借助StructureMap来生成controll ...
随机推荐
- golang 栈操作
Monk's Love for Food Our monk loves food. Hence,he took up position of a manager at Sagar,a restau ...
- Md2All
微信公众号:颜家大少欢迎关注我,一起学习,一起进步!目前,知到 Md2All 的朋友还很少,如果你觉得有帮助,希望能告诉身边有需要的朋友. 谢谢! Md2All 简介 一个Markdown在线转换工具 ...
- npm安装删除模块以及cnpm淘宝镜像
npm安装模块 [$ npm install xxx]利用 npm 安装xxx模块到当前命令行所在目录: [$ npm install -g xxx]利用npm安装全局模块xxx: npm 删除模块 ...
- 安卓电量优化之WakeLock锁机制全面解析
版权声明:本文出自汪磊的博客,转载请务必注明出处. 一.WakeLock概述 wakelock是一种锁的机制,只要有应用拿着这个锁,CPU就无法进入休眠状态,一直处于工作状态.比如,手机屏幕在屏幕关闭 ...
- 【转】NO.3、python+appium+ios,遍历真机元素,得到webview
pyhton+appium+iOS,遍历真机webview.是遍历真机的webview,遍历模拟器的webview请另寻方法. 1.mac上安装ios_webkit_debug_proxy 命令:br ...
- python 豆瓣采集
新手今天刚学python~~~ 有点凌乱~勉强看吧 只能算是给新手看看,见谅 简单版本的 豆瓣采集美图~~~~~~ 美女天天有 有木有~~~ python 3.4 sqlite3 BeautifulS ...
- wifipineapple的evilportal
复制模块: scp -r evilportal root@172.16.42.1:/pineapple/components/infusions 更新数据源, 更新相关依赖: opkg update ...
- bridged(桥接模式)、NAT(网络地址转换模式)和host-only(主机模式)-VMware下三种网络配置方式
VMWare提供了三种工作模式,它们是bridged(桥接模式).NAT(网络地址转换模式)和host-only(主机模式).要想在网络管理和维护中合理应用它们,你就应该先了解一下这三种工作模式. 1 ...
- if与while相互嵌套,菱形*的实现.py
""" * * * * * * * * * * * * * * * * * * * * ...
- 积累jquery一些有意思的函数
$("#btn").unbind("click"); // 让btn这个元素的点击事件失效 $("#btn").unbind(); // 让 ...