这个库提供了在启动期间实例化已注册的单例,而不是在首次使用它时实例化。

单例通常在首次使用时创建,这可能会导致响应传入请求的延迟高于平时。在注册时创建实例有助于防止第一次Request请求的SLA

以往我们要在注册的时候启动单例可能会这样写:

//注册:
services.AddSingleton<FileChangeNotifier>();
//初始化
using var scope = services.BuildServiceProvider().CreateScope();
scope.ServiceProvider.GetRequiredService<FileChangeNotifier>();

但是借助Microsoft.Extensions.DependencyInjection.AutoActivation 我们的写法就特别的简单了:

//注册服务,并直接实例化
services.AddActivatedSingleton<FileChangeNotifier>();

AutoActivation扩展库其实相当简单,内部实现了一个AutoActivationHostedService的 HostedService,当系统启动的时候就从IServiceProvider中取到所有注册为AutoActivation的单例,下面是他的源码:

internal sealed class AutoActivationHostedService : IHostedService
{
private readonly AutoActivatorOptions _options;
private readonly IServiceProvider _provider; public AutoActivationHostedService(IServiceProvider provider, IOptions<AutoActivatorOptions> options)
{
_provider = provider;
_options = Throw.IfMemberNull(options, options.Value);
} public Task StartAsync(CancellationToken cancellationToken)
{
foreach (var singleton in _options.AutoActivators)
{
_ = _provider.GetRequiredService(singleton);
} foreach (var (serviceType, serviceKey) in _options.KeyedAutoActivators)
{
_ = _provider.GetRequiredKeyedService(serviceType, serviceKey);
} return Task.CompletedTask;
} public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}

当然提供的扩展不限于AddActivatedSingleton<T>一个,还有如下的扩展方法:

public static IServiceCollection ActivateSingleton<TService>(this IServiceCollection services)
public static IServiceCollection ActivateSingleton(this IServiceCollection services, Type serviceType)
public static IServiceCollection AddActivatedSingleton<TService, TImplementation>(this IServiceCollection services, Func<IServiceProvider, TImplementation> implementationFactory)
public static IServiceCollection AddActivatedSingleton<TService, TImplementation>(this IServiceCollection services)
public static IServiceCollection AddActivatedSingleton<TService>(this IServiceCollection services, Func<IServiceProvider, TService> implementationFactory)
public static IServiceCollection AddActivatedSingleton<TService>(this IServiceCollection services)
public static IServiceCollection AddActivatedSingleton(this IServiceCollection services, Type serviceType)
public static IServiceCollection AddActivatedSingleton(this IServiceCollection services, Type serviceType, Func<IServiceProvider, object> implementationFactory)
public static IServiceCollection AddActivatedSingleton(this IServiceCollection services, Type serviceType, Type implementationType)
public static void TryAddActivatedSingleton(this IServiceCollection services, Type serviceType)
public static void TryAddActivatedSingleton(this IServiceCollection services, Type serviceType, Type implementationType)
public static void TryAddActivatedSingleton(this IServiceCollection services, Type serviceType, Func<IServiceProvider, object> implementationFactory)
public static void TryAddActivatedSingleton<TService>(this IServiceCollection services)
public static void TryAddActivatedSingleton<TService, TImplementation>(this IServiceCollection services)
public static void TryAddActivatedSingleton<TService>(this IServiceCollection services, Func<IServiceProvider, TService> implementationFactory) public static IServiceCollection ActivateKeyedSingleton<TService>(this IServiceCollection services, object? serviceKey)
public static IServiceCollection ActivateKeyedSingleton(this IServiceCollection services, Type serviceType, object? serviceKey)
public static IServiceCollection AddActivatedKeyedSingleton<TService, TImplementation>(this IServiceCollection services, object? serviceKey, Func<IServiceProvider, object?, TImplementation> implementationFactory)
public static IServiceCollection AddActivatedKeyedSingleton<TService, TImplementation>(this IServiceCollection services, object? serviceKey)
public static IServiceCollection AddActivatedKeyedSingleton<TService>(this IServiceCollection services, object? serviceKey, Func<IServiceProvider, object?, TService> implementationFactory)
public static IServiceCollection AddActivatedKeyedSingleton<TService>(this IServiceCollection services, object? serviceKey)
public static IServiceCollection AddActivatedKeyedSingleton(this IServiceCollection services, Type serviceType, object? serviceKey)
public static IServiceCollection AddActivatedKeyedSingleton(this IServiceCollection services, Type serviceType, object? serviceKey, Func<IServiceProvider, object?, object> implementationFactory)
public static IServiceCollection AddActivatedKeyedSingleton(this IServiceCollection services, Type serviceType, object? serviceKey, Type implementationType)
public static void TryAddActivatedKeyedSingleton(this IServiceCollection services, Type serviceType, object? serviceKey)
public static void TryAddActivatedKeyedSingleton(this IServiceCollection services, Type serviceType, object? serviceKey, Type implementationType)
public static void TryAddActivatedKeyedSingleton(this IServiceCollection services, Type serviceType, object? serviceKey, Func<IServiceProvider, object?, object> implementationFactory)
public static void TryAddActivatedKeyedSingleton<TService>(this IServiceCollection services, object? serviceKey)
public static void TryAddActivatedKeyedSingleton<TService, TImplementation>(this IServiceCollection services, object? serviceKey)
public static void TryAddActivatedKeyedSingleton<TService>(this IServiceCollection services, object? serviceKey, Func<IServiceProvider, object?, TService> implementationFactory)

最近我在Biwen.Settings添加了对JSON配置监听的功能,有这方面的需求,最初是有一个Start方法,也就是启动系统的时候获取服务然后调用Start方法,如果使用了AutoActivation 那Start的方法体可以直接使用构造函数替代,这个也是除了开头解决SLA以外的一个用例吧

有任何不足的地方欢迎小伙伴们留言指正

NET8中增加的简单适用的DI扩展库Microsoft.Extensions.DependencyInjection.AutoActivation的更多相关文章

  1. Microsoft.Extensions.DependencyInjection中的Transient依赖注入关系,使用不当会造成内存泄漏

    Microsoft.Extensions.DependencyInjection中(下面简称DI)的Transient依赖注入关系,表示每次DI获取一个全新的注入对象.但是使用Transient依赖注 ...

  2. ASP.NET Core Web 应用程序系列(三)- 在ASP.NET Core中使用Autofac替换自带DI进行构造函数和属性的批量依赖注入(MVC当中应用)

    在上一章中主要和大家分享了在ASP.NET Core中如何使用Autofac替换自带DI进行构造函数的批量依赖注入,本章将和大家继续分享如何使之能够同时支持属性的批量依赖注入. 约定: 1.仓储层接口 ...

  3. ASP.NET Core Web 应用程序系列(二)- 在ASP.NET Core中使用Autofac替换自带DI进行批量依赖注入(MVC当中应用)

    在上一章中主要和大家分享在MVC当中如何使用ASP.NET Core内置的DI进行批量依赖注入,本章将继续和大家分享在ASP.NET Core中如何使用Autofac替换自带DI进行批量依赖注入. P ...

  4. .NET Core 中的 Logging 简单实用 - 记录日志消息显示到控制台

    .NET Core 支持适用于各种内置和第三方日志记录提供程序的日志记录 API. 本文介绍了如何将日志记录 API 与内置提供程序一起使用. 本文中所述的大多数代码示例都来自 .Net 5 应用. ...

  5. laravel中的自定义函数的加载和第三方扩展库加载

    l 1. 创建文件 app/Helpers/functions.php <?php // 示例函数 function foo() { return "foo"; } 2. 修 ...

  6. asp.net mvc 中 一种简单的 URL 重写

    asp.net mvc 中 一种简单的 URL 重写 Intro 在项目中想增加一个公告的功能,但是又不想直接用默认带的那种路由,感觉好low逼,想弄成那种伪静态化的路由 (别问我为什么不直接静态化, ...

  7. CentOS6.4 安装OpenResty和Redis 并在Nginx中利用lua简单读取Redis数据

    1.下载OpenResty和Redis OpenResty下载地址:wget http://openresty.org/download/ngx_openresty-1.4.3.6.tar.gz Re ...

  8. javascrit2.0完全参考手册(第二版) 第1章第1节 在XHTML文档中增加javascript

    通常,向文档中增加script脚本使用<script>元素,在HTML中增加脚本的方式有4中: (1)放到<script></script>块中: (2)<s ...

  9. iOS开发UI篇—iOS开发中三种简单的动画设置

    iOS开发UI篇—iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView b ...

  10. [转]通过继承ConfigurationSection,在web.config中增加自定义配置

    本文转自:http://www.blue1000.com/bkhtml/2008-02/55810.htm 前几天写了一篇使用IConfigurationSectionHandler在web.conf ...

随机推荐

  1. 基于 eBPF 的 Kubernetes 可观测实践

    简介: 阿里云可观测团队构建了 kubernetes 统一监控,无侵入式地提供多语言.应用性能黄金指标,支持多种协议,结合 Kubernetes 管控层与网络系统层监控,提供全栈一体式的可观测体验.通 ...

  2. 成本节省 50%,9人团队使用函数计算开发 wolai 在线文档应用

    简介: 通过使用函数计算,wolai 的前端工程师们就可以把从前到后的一整套开发流程负责起来,我们的研发迭代速度非常快.   ​ 作者| 马锐拉(wolai.com 创始人) 我们的日常工作场景几乎离 ...

  3. 阿里云RDS深度定制-XA Crash Safe

    ​简介: 近几年,随着分布式数据库系统的兴起,特别是基于MySQL分布式数据库系统,会用到XA来保证全局事务的一致性.众所周知,MySQL对XA事务的支持是比较弱的,存在很多问题.为了满足分布式数据库 ...

  4. 当微服务遇上 Serverless | 微服务容器化最短路径,微服务 on Serverless 最佳实践

    ​简介: 阿里云Serverless应用引擎(SAE)初衷是让客户不改任何代码,不改变应用部署方式,就可以享受到微服务+K8s+Serverless的完整体验,开箱即用免运维. 前言 微服务作为一种更 ...

  5. dotnet 已知问题 错误标记 MethodImplOptions.InternalCall 特性参数将会在类型访问之前抛出 TypeLoadException 异常

    本文将记录一个 dotnet 的已知问题.当自己不小心在方法上不正确标记了 MethodImplAttribute 特性时,错误选择了 MethodImplOptions.InternalCall 参 ...

  6. Win32 使用 CreateProcess 方法让任务管理器里的命令行不显示应用文件路径

    本文记录一个 Win32 的有趣行为,调用 CreateProcess 方法传入特别的参数,可以让任务管理器里的命令行不显示应用文件路径 开始之前,先看看下面这张有趣的图片 可以看到我编写的 Svca ...

  7. STM32F10X 部分引脚不能使用的问题

    一.概述 说来惭愧,我到现在都没有完整的学习过STM32.接触 STM32 还是突然的一个项目,需要用到 STM32,紧急需求,只能边学边完成.不过好在 ST 的资料还是比较多的,相对也比较简单,基本 ...

  8. 如何在M1 MACBOOK上准备好ORB-SLAM2的环境

    1. 环境 M1 Macbook Air Parallels Desktop 17 关于虚拟机,在之前尝试了UTM,但是性能欠佳,卡顿情况比较多,而且未能解决联网问题,最终只能含泪放弃 Paralle ...

  9. .Net 线程与锁

    一台服务器能运行多少个线程,大致取决于CPU的管理能力.CPU负责线程的创建.协调.切换.销毁.暂停.唤醒.运行等.一个应用程序中,必须有一个进程维持应用程序的运行环境,一个进程可同时有多个线程协作处 ...

  10. golang向上取整、向下取整和四舍五入

    一.概述 官方的math 包中提供了取整的方法,向上取整math.Ceil() ,向下取整math.Floor() 二.用法 package main import ( "fmt" ...