这个库提供了在启动期间实例化已注册的单例,而不是在首次使用它时实例化。
单例通常在首次使用时创建,这可能会导致响应传入请求的延迟高于平时。在注册时创建实例有助于防止第一次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以外的一个用例吧
有任何不足的地方欢迎小伙伴们留言指正