1
0

Removing BackgroundServiceStarter from AddSingletonHostedService

This commit is contained in:
2023-11-30 21:43:28 +01:00
parent 94c02c2a89
commit db9ddecff3
3 changed files with 13 additions and 48 deletions

View File

@@ -18,7 +18,8 @@ namespace Microsoft.Extensions.DependencyInjection
where TService : class, IHostedService
{
services.AddSingleton<TService>();
services.AddSingleton<IHostedService, BackgroundServiceStarter<TService>>();
services.AddHostedService(serviceProvider => serviceProvider.GetRequiredService<TService>());
return services;
}
@@ -30,10 +31,11 @@ namespace Microsoft.Extensions.DependencyInjection
/// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IServiceCollection AddSingletonHostedService<TService, TImplementation>(this IServiceCollection services)
where TService : class, IHostedService where TImplementation : class, TService
where TService : class, IHostedService
where TImplementation : class, TService
{
services.AddSingleton<TService, TImplementation>();
services.AddSingleton<IHostedService, BackgroundServiceStarter<TService>>();
services.AddHostedService(serviceProvider => serviceProvider.GetRequiredService<TService>());
return services;
}