重新整理.net core 计1400篇[九] (.net core 中的依赖注入的服务注入)
前言
在该系列六中介绍了一个简单的依赖注入,该节介绍.net core 中的依赖注入的服务注入。
ServiceDescriptor
ServiceDescriptor 是服务描述的意思,这个是做什么的呢?
我们知道当我们要实例化一个服务的时候,我们通过serviceType 属性去查找,是否我们注册了。
那么通过serviceType 查找到的是什么呢?就是这个ServiceDescriptor,好了那么分析一下这个serviceDescriptor 到底是什么吧。
来看一下又什么属性吧!
public ServiceLifetime Lifetime
{
[CompilerGenerated]
get;
}
/// <inheritdoc />
public Type ServiceType
{
[CompilerGenerated]
get;
}
/// <inheritdoc />
public Type ImplementationType
{
[CompilerGenerated]
get;
}
/// <inheritdoc />
public object ImplementationInstance
{
[CompilerGenerated]
get;
}
/// <inheritdoc />
public Func<IServiceProvider, object> ImplementationFactory
{
[CompilerGenerated]
get;
}
下面来介绍一下属性。
Lifetime 这个我想不用介绍了吧,生命周期。
public enum ServiceLifetime
{
/// <summary>
/// Specifies that a single instance of the service will be created.
/// </summary>
Singleton,
/// <summary>
/// Specifies that a new instance of the service will be created for each scope.
/// </summary>
/// <remarks>
/// In ASP.NET Core applications a scope is created around each server request.
/// </remarks>
Scoped,
/// <summary>
/// Specifies that a new instance of the service will be created every time it is requested.
/// </summary>
Transient
}
ServiceType,我想我也不用介绍的,就是当前注入类型,比如说IFoo
然后呢,下面三种属性体现了,提供服务实例的三种方式:
/// <inheritdoc />
public Type ImplementationType
{
[CompilerGenerated]
get;
}
/// <inheritdoc />
public object ImplementationInstance
{
[CompilerGenerated]
get;
}
/// <inheritdoc />
public Func<IServiceProvider, object> ImplementationFactory
{
[CompilerGenerated]
get;
}
这个需要我们来看下这个的时候ServiceDescriptor的构造函数。
/// <summary>
/// Initializes a new instance of <see cref="T:Microsoft.Extensions.DependencyInjection.ServiceDescriptor" /> with the specified <paramref name="implementationType" />.
/// </summary>
/// <param name="serviceType">The <see cref="T:System.Type" /> of the service.</param>
/// <param name="implementationType">The <see cref="T:System.Type" /> implementing the service.</param>
/// <param name="lifetime">The <see cref="T:Microsoft.Extensions.DependencyInjection.ServiceLifetime" /> of the service.</param>
public ServiceDescriptor(Type serviceType, Type implementationType, ServiceLifetime lifetime)
: this(serviceType, lifetime)
{
//IL_0016: Unknown result type (might be due to invalid IL or missing references)
//IL_002a: Unknown result type (might be due to invalid IL or missing references)
if (serviceType == (Type)null)
{
throw new ArgumentNullException("serviceType");
}
if (implementationType == (Type)null)
{
throw new ArgumentNullException("implementationType");
}
ImplementationType = implementationType;
}
/// <summary>
/// Initializes a new instance of <see cref="T:Microsoft.Extensions.DependencyInjection.ServiceDescriptor" /> with the specified <paramref name="instance" />
/// as a <see cref="F:Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton" />.
/// </summary>
/// <param name="serviceType">The <see cref="T:System.Type" /> of the service.</param>
/// <param name="instance">The instance implementing the service.</param>
public ServiceDescriptor(Type serviceType, object instance)
: this(serviceType, ServiceLifetime.Singleton)
{
//IL_0016: Unknown result type (might be due to invalid IL or missing references)
//IL_0024: Unknown result type (might be due to invalid IL or missing references)
if (serviceType == (Type)null)
{
throw new ArgumentNullException("serviceType");
}
if (instance == null)
{
throw new ArgumentNullException("instance");
}
ImplementationInstance = instance;
}
/// <summary>
/// Initializes a new instance of <see cref="T:Microsoft.Extensions.DependencyInjection.ServiceDescriptor" /> with the specified <paramref name="factory" />.
/// </summary>
/// <param name="serviceType">The <see cref="T:System.Type" /> of the service.</param>
/// <param name="factory">A factory used for creating service instances.</param>
/// <param name="lifetime">The <see cref="T:Microsoft.Extensions.DependencyInjection.ServiceLifetime" /> of the service.</param>
public ServiceDescriptor(Type serviceType, Func<IServiceProvider, object> factory, ServiceLifetime lifetime)
: this(serviceType, lifetime)
{
//IL_0016: Unknown result type (might be due to invalid IL or missing references)
//IL_0024: Unknown result type (might be due to invalid IL or missing references)
if (serviceType == (Type)null)
{
throw new ArgumentNullException("serviceType");
}
if (factory == null)
{
throw new ArgumentNullException("factory");
}
ImplementationFactory = factory;
}
我们看到了这三个构造方法,那么我们基本明白了,到时候创建服务实例的时候就是这几种方法了。
这里面有一个值得注意的地方,就是:
public ServiceDescriptor(Type serviceType, object instance)
: this(serviceType, ServiceLifetime.Singleton)
{
//IL_0016: Unknown result type (might be due to invalid IL or missing references)
//IL_0024: Unknown result type (might be due to invalid IL or missing references)
if (serviceType == (Type)null)
{
throw new ArgumentNullException("serviceType");
}
if (instance == null)
{
throw new ArgumentNullException("instance");
}
ImplementationInstance = instance;
}
也就是说我们可以直接让接口对应实例,比如说:new ServiceDescriptor(IFoo,new Foo());默认的生命周期模式就是:ServiceLifetime.Singleton
IServiceCollection
依赖存储框架将服务注册存储在一个通过IServiceCollection 接口表示的集合中。
public interface IServiceCollection : IList<ServiceDescriptor>, ICollection<ServiceDescriptor>, IEnumerable<ServiceDescriptor>, IEnumerable```
{
}
在 IServiceCollection 中,本质上其实就是一个ServiceDescriptor集合。
那么来看下IServiceCollection的实例对象ServiceCollection的扩展方法ServiceCollectionServiceExtensions。
private static IServiceCollection Add(IServiceCollection collection, Type serviceType, Type implementationType, ServiceLifetime lifetime)
{
ServiceDescriptor serviceDescriptor = new ServiceDescriptor(serviceType, implementationType, lifetime);
((ICollection<ServiceDescriptor>)collection).Add(serviceDescriptor);
return collection;
}
private static IServiceCollection Add(IServiceCollection collection, Type serviceType, Func<IServiceProvider, object> implementationFactory, ServiceLifetime lifetime)
{
ServiceDescriptor serviceDescriptor = new ServiceDescriptor(serviceType, implementationFactory, lifetime);
((ICollection<ServiceDescriptor>)collection).Add(serviceDescriptor);
return collection;
}
可以帮助我们直接创建ServiceDescriptor,并且加入到当前的ServiceCollection 中。
然后ServiceCollectionServiceExtensions 在3.0在还更新了,tryadd,因为我们一个ServiceType 可以对应多个,使用tryadd 的时候,就会尝试加入,如果有的话就不会加入。
在ServiceCollection和ServiceCollectionServiceExtensions 有一些其他的方法,最好去看一下源码,了解一下。
总结
下一章依赖注入的服务消费
重新整理.net core 计1400篇[九] (.net core 中的依赖注入的服务注入)的更多相关文章
- ASP.NET CORE MVC 2.0 如何在Filter中使用依赖注入来读取AppSettings,及.NET Core控制台项目中读取AppSettings
问: ASP.NET CORE MVC 如何在Filter中使用依赖注入来读取AppSettings 答: Dependency injection is possible in filters as ...
- ASP.NET Core 依赖注入(构造函数注入,属性注入等)
原文:ASP.NET Core 依赖注入(构造函数注入,属性注入等) 如果你不熟悉ASP.NET Core依赖注入,先阅读文章: 在ASP.NET Core中使用依赖注入 构造函数注入 构造函数注 ...
- ASP.NET Core中的依赖注入【上】
此为系列文章,对MSDN ASP.NET Core 的官方文档进行系统学习与翻译.其中或许会添加本人对 ASP.NET Core 的浅显理解 ASP.NET Core支持DI软件设计模式,其是一种为了 ...
- 重新整理 .net core 周边阅读篇————AspNetCoreRateLimit[一]
前言 整理了一下.net core 一些常见的库的源码阅读,共32个库,记100余篇. 以下只是个人的源码阅读,如有错误或者思路不正确,望请指点. 正文 github 地址为: https://git ...
- 鸿蒙内核源码分析(进程通讯篇) | 九种进程间通讯方式速揽 | 百篇博客分析OpenHarmony源码 | v28.03
百篇博客系列篇.本篇为: v28.xx 鸿蒙内核源码分析(进程通讯篇) | 九种进程间通讯方式速揽 | 51.c.h .o 进程通讯相关篇为: v26.xx 鸿蒙内核源码分析(自旋锁篇) | 自旋锁当 ...
- (Hibernate进阶)Hibernate系列——总结篇(九)
这篇博文是hibernate系列的最后一篇,既然是最后一篇,我们就应该进行一下从头到尾,整体上的总结,将这个系列的内容融会贯通. 概念 Hibernate是一个对象关系映射框架,当然从分层的角度看,我 ...
- 使用Visual Studio Code开发.NET Core看这篇就够了
作者:依乐祝 原文地址:https://www.cnblogs.com/yilezhu/p/9926078.html 在本文中,我将带着大家一步一步的通过图文的形式来演示如何在Visual Studi ...
- .NET Core实战项目之CMS 第二章 入门篇-快速入门ASP.NET Core看这篇就够了
作者:依乐祝 原文链接:https://www.cnblogs.com/yilezhu/p/9985451.html 本来这篇只是想简单介绍下ASP.NET Core MVC项目的(毕竟要照顾到很多新 ...
- .NET Core实战项目之CMS 第三章 入门篇-源码解析配置文件及依赖注入
作者:依乐祝 原文链接:https://www.cnblogs.com/yilezhu/p/9998021.html 写在前面 上篇文章我给大家讲解了ASP.NET Core的概念及为什么使用它,接着 ...
- ASP.NET Core 2.2 十九. 你扔过来个json,我怎么接
原文:ASP.NET Core 2.2 十九. 你扔过来个json,我怎么接 前文说道了Action的激活,这里有个关键的操作就是Action参数的映射与模型绑定,这里即涉及到简单的string.in ...
随机推荐
- tp5.1 controller 名称自动转换大小写,导致文件名对不上 url_convert
// 是否自动转换URL中的控制器和操作名 'url_convert' => false, // true,
- linux-关于conio.h文件的文件缺失问题
链接: https://pan.baidu.com/s/1Qzo4CkJB1_5E-3rDLtfG4Q 提取码: fh65 编辑以下这个依赖库就可以了 $ cd libconio-1.0.0 $ ./ ...
- 关于初始化page入参的设计思路
最近在重构老的代码,在写的过程中发现之前的逻辑如果遇到没有入参pageNo会Npe,于是乎我想找找公司项目有啥方式处理page入参的有两种如下 使用三元表达式直接判断是否null,然后赋值 使用map ...
- 【jQuery】(8)---jquery Ajax
jquery ajax 一.AJAX 概述 1.什么是 AJAX? 特点 AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使 ...
- decltype总结
decltype会识别const和引用 decltype一个表达式的时候会判断该表达式是左值还是右值,如果是左值就返回引用,否则就返回非引用. int &a = xx; const declt ...
- C++小细节
cin不仅遇到EOF会返回无效状态(通常用来终止循环),遇到无效输入的时候也会返回无效状态,比如向整型变量输入字符. char类型的大小和机器有关,最小8位,大多数机器字节(byte)是8位,byte ...
- STM32 启动代码分析
PS:要转载请注明出处,本人版权所有. PS: 这个只是基于<我自己>的理解, 如果和你的原则及想法相冲突,请谅解,勿喷. 前置说明 本文作为本人csdn blog的主站的备份.(Bl ...
- Walrus 0.6发布:预览资源变更、丰富公有云支持,满足企业多云需求
近日,数澈软件Seal(以下简称"Seal")宣布基于 IaC 的开源应用管理平台 Walrus 0.6 正式发布! 在之前的版本中,Walrus 引入应用模型并优化了应用部署体验 ...
- 03.Java数据结构问题
目录介绍 3.0.0.1 在arrayList中System.arraycopy()和Arrays.copyOf()方法区别联系?System.arraycopy()和Arrays.copyOf()代 ...
- 阿里二面:谈谈ThreadLocal的内存泄漏问题?问麻了。。。。
引言 ThreadLocal在Java多线程编程中扮演着重要的角色,它提供了一种线程局部存储机制,允许每个线程拥有独立的变量副本,从而有效地避免了线程间的数据共享冲突.ThreadLocal的主要用途 ...