« Background task » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
Aucun résumé des modifications
Ligne 28 : Ligne 28 :
services.AddHostedService<MyBackgroundService>();
services.AddHostedService<MyBackgroundService>();
services.AddScoped<IMyScopedService, MyScopedService>();
services.AddScoped<IMyScopedService, MyScopedService>();
</filebox>
= [https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?tabs=visual-studio#queued-background-tasks Queued background tasks] =
<filebox fn='QueuedHostedService.cs'>
public class QueuedHostedService(IBackgroundTaskQueue taskQueue) : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            var workItem = await TaskQueue.DequeueAsync(stoppingToken);
            try
            {
                await workItem(stoppingToken);
            }
            catch (Exception ex)
            { }
        }
    }
}
</filebox>
<filebox fn='Program.cs'>
services.AddHostedService<QueuedHostedService>();
services.AddSingleton<IBackgroundTaskQueue>(ctx =>
{
    if (!int.TryParse(hostContext.Configuration["QueueCapacity"], out var queueCapacity))
        queueCapacity = 100;
    return new BackgroundTaskQueue(queueCapacity);
});
</filebox>
</filebox>


= [https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?tabs=visual-studio#timed-background-tasks Timed background tasks] =
= [https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?tabs=visual-studio#timed-background-tasks Timed background tasks] =

Version du 17 septembre 2024 à 13:49

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 scopedService = scope.ServiceProvider.GetRequiredService<IMyScopedService>();
            await scopedService.DoWork(stoppingToken);
        }
    }

    public override async Task StopAsync(CancellationToken stoppingToken)
    {
        await base.StopAsync(stoppingToken);
    }
}
Program.cs
services.AddHostedService<MyBackgroundService>();
services.AddScoped<IMyScopedService, MyScopedService>();

Queued background tasks

QueuedHostedService.cs
public class QueuedHostedService(IBackgroundTaskQueue taskQueue) : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            var workItem = await TaskQueue.DequeueAsync(stoppingToken);

            try
            {
                await workItem(stoppingToken);
            }
            catch (Exception ex)
            { }
        }
    }
}
Program.cs
services.AddHostedService<QueuedHostedService>();
services.AddSingleton<IBackgroundTaskQueue>(ctx =>
{
    if (!int.TryParse(hostContext.Configuration["QueueCapacity"], out var queueCapacity))
        queueCapacity = 100;
    return new BackgroundTaskQueue(queueCapacity);
});

Timed background tasks