NET8中增加的简单适用的DI扩展库Microsoft.Extensions.DependencyInjection.AutoActivation

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

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

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

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

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

csharp 复制代码
//注册服务,并直接实例化
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>一个,还有如下的扩展方法:

csharp 复制代码
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以外的一个用例吧

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

相关推荐
EIP低代码平台3 天前
EIP低代码平台 - 应用管理 - 表单设计
低代码·c#·权限·工作流·netcore
EIP低代码平台5 天前
EIP低代码平台 - 系统日志
低代码·c#·权限·工作流·netcore
EIP低代码平台5 天前
EIP 低代码平台 - 角色维护
低代码·c#·权限·工作流·netcore
EIP低代码平台8 天前
EIP低代码平台-系统参数配置详解|落地三级等保+高灵活私有化部署方案
低代码·c#·权限·工作流·netcore
程序设计实验室6 个月前
AspNetCore开发笔记:WebApi项目集成企业微信和公众号
aspnetcore
TypingLearn7 个月前
Perigon.CLI 10.0 重磅发布【AspNetCore开发模板和辅助工具】
c#·.net·aspnetcore
TypingLearn7 个月前
2026年,让.NET再次伟大
windows·c#·.net·sdk·netcore
程序设计实验室1 年前
StarBlog v1.3.0 新版本,一大波更新以及迁移服务器部署
c#·aspnetcore·starblog番外
ggtc1 年前
新能源电池壳冲压车间看板实施
vue·netcore·看板·车间
ggtc1 年前
冲压车间软件实施
vue·netcore·工厂