前言

在该系列六中介绍了一个简单的依赖注入,该节介绍.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 中的依赖注入的服务注入)的更多相关文章

  1. ASP.NET CORE MVC 2.0 如何在Filter中使用依赖注入来读取AppSettings,及.NET Core控制台项目中读取AppSettings

    问: ASP.NET CORE MVC 如何在Filter中使用依赖注入来读取AppSettings 答: Dependency injection is possible in filters as ...

  2. ASP.NET Core 依赖注入(构造函数注入,属性注入等)

    原文:ASP.NET Core 依赖注入(构造函数注入,属性注入等) 如果你不熟悉ASP.NET Core依赖注入,先阅读文章: 在ASP.NET Core中使用依赖注入   构造函数注入 构造函数注 ...

  3. ASP.NET Core中的依赖注入【上】

    此为系列文章,对MSDN ASP.NET Core 的官方文档进行系统学习与翻译.其中或许会添加本人对 ASP.NET Core 的浅显理解 ASP.NET Core支持DI软件设计模式,其是一种为了 ...

  4. 重新整理 .net core 周边阅读篇————AspNetCoreRateLimit[一]

    前言 整理了一下.net core 一些常见的库的源码阅读,共32个库,记100余篇. 以下只是个人的源码阅读,如有错误或者思路不正确,望请指点. 正文 github 地址为: https://git ...

  5. 鸿蒙内核源码分析(进程通讯篇) | 九种进程间通讯方式速揽 | 百篇博客分析OpenHarmony源码 | v28.03

    百篇博客系列篇.本篇为: v28.xx 鸿蒙内核源码分析(进程通讯篇) | 九种进程间通讯方式速揽 | 51.c.h .o 进程通讯相关篇为: v26.xx 鸿蒙内核源码分析(自旋锁篇) | 自旋锁当 ...

  6. (Hibernate进阶)Hibernate系列——总结篇(九)

    这篇博文是hibernate系列的最后一篇,既然是最后一篇,我们就应该进行一下从头到尾,整体上的总结,将这个系列的内容融会贯通. 概念 Hibernate是一个对象关系映射框架,当然从分层的角度看,我 ...

  7. 使用Visual Studio Code开发.NET Core看这篇就够了

    作者:依乐祝 原文地址:https://www.cnblogs.com/yilezhu/p/9926078.html 在本文中,我将带着大家一步一步的通过图文的形式来演示如何在Visual Studi ...

  8. .NET Core实战项目之CMS 第二章 入门篇-快速入门ASP.NET Core看这篇就够了

    作者:依乐祝 原文链接:https://www.cnblogs.com/yilezhu/p/9985451.html 本来这篇只是想简单介绍下ASP.NET Core MVC项目的(毕竟要照顾到很多新 ...

  9. .NET Core实战项目之CMS 第三章 入门篇-源码解析配置文件及依赖注入

    作者:依乐祝 原文链接:https://www.cnblogs.com/yilezhu/p/9998021.html 写在前面 上篇文章我给大家讲解了ASP.NET Core的概念及为什么使用它,接着 ...

  10. ASP.NET Core 2.2 十九. 你扔过来个json,我怎么接

    原文:ASP.NET Core 2.2 十九. 你扔过来个json,我怎么接 前文说道了Action的激活,这里有个关键的操作就是Action参数的映射与模型绑定,这里即涉及到简单的string.in ...

随机推荐

  1. InputRegZen.vue 正则Input 限制输入框输入内容

    核心内容 已经 perfect,没有用外库,原生完成 用的 iview的Input组件 封装 // InputRegZen.vue <template> <div> <I ...

  2. VS Code Snippet Generator 插件 生成 vscode代码片段

    VS Code Snippet Generator 插件 生成 vscode代码片段

  3. finger 单词学习 词源通 five (penkwe)

    印欧语penkwe - finger p通f 元音i通e 或者说从e降级到i (aeiou) n保持不变 k通g we 怎么转的 er 我也不知道,不嫌麻烦就是 w -> m -> n - ...

  4. C++数值类型与string、CString之间的转换

    目录 数值范围 数值类型与string互相转换 数值类型转换为string 使用函数模板+ostringstream 使用标准库函数std::to_string() string转换为数值类型 使用函 ...

  5. manjaro安装/卸载gnome/kde桌面环境

    安装gnome桌面环境 步骤 1. 在运行以下教程之前,请确保我们的系统是最新的: sudo pacman -Syu 步骤 2. 在 Manjaro 20 上安装 GNOME 桌面. 现在我们通过执行 ...

  6. 详解在 centos 中引导到救援模式

    详解在 centos 中引导到救援模式 Linux系统CentOS进入单用户模式和救援模式详解 一.概述 目前在运维日常工作中,经常会遇到服务器异常断电.忘记root密码.系统引导文件损坏无法进入系统 ...

  7. 实时3D渲染它是如何工作的?可以在哪些行业应用?

    随着新兴技术--3D渲染的发展,交互应用的质量有了极大的提高.用实时三维渲染软件创建的沉浸式数字体验,几乎与现实没有区别了.随着技术的逐步改进,在价格较低的个人工作站上渲染3D图像变得更加容易,设计师 ...

  8. [深度学习] 计算机视觉低代码工具Supervision库使用指北

    Supervision库是一款出色的Python计算机视觉低代码工具,其设计初衷在于为用户提供一个便捷且高效的接口,用以处理数据集以及直观地展示检测结果.Supervision库的官方开源仓库地址为: ...

  9. (3)安装完python之后需要安装的Spyder集成开发环境教程

    步骤一: 首先,在网站上下载你所需要的压缩文件,网址为https://files.pythonhosted.org/packages/5e/a0/ab7f29e32479d15663eab9afd1d ...

  10. 记录--20行js就能实现逐字显示效果???-打字机效果

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 效果演示 横版 竖版 思路分析 可以看到文字是一段一段的并且独占一行,使用段落标签p表示一行 一段文字内,字是一个一个显示的,所以这里每一 ...