Background task
De Banane Atomic
Aller à la navigationAller à la recherche
Links
Description
In ASP.NET Core, background tasks can be implemented as hosted services.
Consuming a scoped service in a background task
MyBackgroundService.cs |
public class MyBackgroundService(IServiceProvider services) : BackgroundService { protected override async Task ExecuteAsync(CancellationToken stoppingToken) { using (var scope = Services.CreateScope()) { var scopedProcessingService = scope.ServiceProvider.GetRequiredService<IScopedProcessingService>(); await scopedProcessingService.DoWork(stoppingToken); } } public override async Task StopAsync(CancellationToken stoppingToken) { await base.StopAsync(stoppingToken); } } |