diff --git a/AMWD.Common.AspNetCore/Extensions/ServiceCollectionExtensions.cs b/AMWD.Common.AspNetCore/Extensions/ServiceCollectionExtensions.cs index 674b07e..5d24a0a 100644 --- a/AMWD.Common.AspNetCore/Extensions/ServiceCollectionExtensions.cs +++ b/AMWD.Common.AspNetCore/Extensions/ServiceCollectionExtensions.cs @@ -20,5 +20,21 @@ namespace Microsoft.Extensions.DependencyInjection services.AddSingleton>(); return services; } + + /// + /// Adds a hosted service that is instanciated only once. + /// + /// The type of the service to add. + /// The type of the implementation of the service to add. + /// The to add the service to. + /// A reference to this instance after the operation has completed. + public static IServiceCollection AddSingletonHostedService(this IServiceCollection services) + where TService : class, IHostedService where TImplementation : class, TService + { + services.AddSingleton(); + services.AddSingleton>(); + + return services; + } } }