Microsoft.Extensions.DependencyInjection.AutoActivation扩展库介绍
扫描二维码随身看资讯
使用手机 二维码应用 扫描右侧二维码,您可以
1. 在手机上细细品读~
2. 分享给您的微信好友或朋友圈~
这个库提供了在 启动期间实例化 已注册的单例, 而不是 在首次使用它时实例化。
单例通常在首次使用时创建,这可能会导致响应传入请求的延迟高于平时。在注册时创建实例有助于防止第一次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以外的一个用例吧。
- 无尽的拉格朗日一号玩家版本下载 v1.2.514975 安卓版
- 仙国志抖音版下载(附兑换码) v1.16.0 安卓版
- 传奇三职业手游下载 v1.0.0 安卓版
- 诛神世界火影0.1折跑酷送代币版下载 v1.0.0 安卓版
- 新不良人天威星服下载 v1.0.241 安卓版
- 我这一刀下去999999钻999999金币下载 v1.0.2 安卓版
- 罗布乐思国际服正版
- 乐鸿捕鱼1000亿倍版本下载 v5.10.4 安卓版
- 绿茵传奇破解内置菜单版下载 v2.7.2 安卓版
- 神火大陆安卓版下载安装 v11.0 安卓版
- 传说法师安卓版
- 我的世界真正的凋零风暴模组整合包 v1.19.2 安卓版
- 自由城计划内置MOD修改版下载 v1.6 安卓版
- 龙兽争霸4无限零件版下载 v1.0.20 安卓版
- RSA算法中为什么需要两个素数?
- 腾讯云 BI 商业智能解决方案快速入门指南
- HTTPS协议安全性原理及与HTTP的比较
- 【译】Visual Studio 17.10 发布了新版扩展管理器
- Python 潮流周刊#55:分享 9 个高质量的技术类信息源!
- 深度优化分布式缓存性能:微软推出.NET9中的HybridCache解决方案
- 一文搞懂 ARM 64 系列: 一文搞懂 ARM 64 系列: 函数调用传参与返回值
- IceRPC之依赖注入>快乐的RPC
- Control API的功能与用法
- FRDM-MCXN947开发板之i2c应用
- Asp .Net Core 系列:详解鉴权(身份验证)以及实现 Cookie、JWT、自定义三种鉴权 (含源码解析)
- 【译】通过出色的开发人员体验,将乐趣最大化,将痛苦最小化
- 1
加查之花 正版
- 2
爪女孩 最新版
- 3
企鹅岛 官方正版中文版
- 4
捕鱼大世界 无限金币版
- 5
情商天花板 2024最新版
- 6
内蒙打大a真人版
- 7
烦人的村民 手机版
- 8
球球英雄 手游
- 9
跳跃之王手游
- 10
蛋仔派对 国服版本