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;
}

View File

@@ -1,45 +0,0 @@
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.Extensions.Hosting
{
/// <summary>
/// Wrapper class to start a background service.
/// </summary>
/// <typeparam name="TService">The service type.</typeparam>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class BackgroundServiceStarter<TService> : IHostedService
where TService : class, IHostedService
{
private readonly TService service;
/// <summary>
/// Initializes an new instance of the <see cref="BackgroundServiceStarter{TService}"/> class.
/// </summary>
/// <param name="backgroundService">The service to work in background.</param>
public BackgroundServiceStarter(TService backgroundService)
{
service = backgroundService;
}
/// <summary>
/// Starts the service.
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task StartAsync(CancellationToken cancellationToken)
{
return service.StartAsync(cancellationToken);
}
/// <summary>
/// Stops the service.
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task StopAsync(CancellationToken cancellationToken)
{
return service.StopAsync(cancellationToken);
}
}
}

View File

@@ -14,6 +14,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [AMWD.Common.EntityFrameworkCore](https://git.am-wd.de/AM.WD/common/compare/efc/v2.0.1...main)
- [AMWD.Common.Test](https://git.am-wd.de/AM.WD/common/compare/test/v2.1.1...main)
### Changed
- Using `AddHostedService<>()` with "implementation factory" for `AddSingletonHostedService<>()` to resove singleton instance
### Removed
- Removed `BackgroundServiceStarter`
## v2.0.1, asp/v2.1.0, efc/v2.0.1, test/v2.1.1 - 2023-11-23