重新整理.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 ...
随机推荐
- Redis系列:RDB内存快照提供持久化能力
★ Redis24篇集合 1 介绍 从上一篇的 <深刻理解高性能Redis的本质> 中可以知道, 我们经常在数据库层上加一层缓存(如Redis),来保证数据的访问效率. 这样性能确实也有了 ...
- Github无法读取远程仓库
主页 个人微信公众号:密码应用技术实战 个人博客园首页:https://www.cnblogs.com/informatics/ Git无法访问 今早起来访问Github炸了,Git不能用了,提示: ...
- Prometheus技术分享——如何监控宿主机和容器
这一期主要来跟大家聊一下,使用node_exporter工具来暴露主机和因公程序上的指标,利用prometheus来监控宿主机:以及通过通过Cadvisor监控docker容器. 一.部署node_e ...
- [VueJsDev] 快速入门 - vscode 设置推荐
[VueJsDev] 目录列表 https://www.cnblogs.com/pengchenggang/p/17037320.html vscode设置推荐 ::: details 目录 目录 v ...
- vue3 markdown 读取文件的两种方法 有gitee发布地址
方法一: markdown-loader html-loader import的时候就转换成html了,每次需要build,但是可以本地双击就能看,放哪个目录页不限制 方法二: axios + mar ...
- 基于Linux的DockerFile添加中文字体
FROM openjdk:8u332-oraclelinux7 #RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk ...
- Android Studio批量打渠道包
原文: Android Studio批量打渠道包 - Stars-One的杂货小窝 公司项目渠道包越来越大,每次发版本都是开发人员打包,研究了下如何批量打渠道包,记录过程 步骤 1.gradle配置 ...
- KTL 一个支持C++14编辑公式的K线技术工具平台 - 第八版,数据解析。附带通达信gbbq解码。
K,K线,Candle蜡烛图. T,技术分析,工具平台 L,公式Language语言使用c++14,Lite小巧简易. 项目仓库:https://github.com/bbqz007/KTL 国内仓库 ...
- 不想dto套dto可以这样写
之前都是要新建个dto文件的,偶然看到别人这样写,简单记录一下 @Data public class GdtDailyBalanceContent { List<GdtDailyBalanceR ...
- 12_采样格式&音频重采样
采样格式 通过前面学习我们知道FFmpeg和SDL都有自己的采样格式的表达式,那么他们都表示什么意思呢? FFmpeg的采样格式的表达式: enum AVCodecID { ...... AV_COD ...