1
0

Erweiterung des SingletonHostedService für Interface + Implementierung.

This commit is contained in:
2021-11-15 00:02:43 +01:00
parent 3502ef44e2
commit f62adb6d72

View File

@@ -20,5 +20,21 @@ namespace Microsoft.Extensions.DependencyInjection
services.AddSingleton<IHostedService, BackgroundServiceStarter<TService>>();
return services;
}
/// <summary>
/// Adds a hosted service that is instanciated only once.
/// </summary>
/// <typeparam name="TService">The type of the service to add.</typeparam>
/// <typeparam name="TImplementation">The type of the implementation of the service to add.</typeparam>
/// <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
{
services.AddSingleton<TService, TImplementation>();
services.AddSingleton<IHostedService, BackgroundServiceStarter<TService>>();
return services;
}
}
}