From f62adb6d725eade2b6fa47a081c2805c01bf14fe Mon Sep 17 00:00:00 2001 From: Andreas Mueller Date: Mon, 15 Nov 2021 00:02:43 +0100 Subject: [PATCH] =?UTF-8?q?Erweiterung=20des=20SingletonHostedService=20f?= =?UTF-8?q?=C3=BCr=20Interface=20+=20Implementierung.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/ServiceCollectionExtensions.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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; + } } }