ASPNET Background Tasks — Running Background Jobs in ASP.NET Core
DodaTech
Updated 2026-06-28
1 min read
In this tutorial, you'll learn about ASPNET Background Tasks. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
ASP.NET Core's hosted services provide a robust way to run background tasks alongside your web application.
public class ScanQueueProcessor : BackgroundService
{
private readonly ILogger<ScanQueueProcessor> _logger;
private readonly IServiceScopeFactory _scopeFactory;
private readonly Channel<ScanRequest> _queue = Channel.CreateUnbounded<ScanRequest>();
public ScanQueueProcessor(ILogger<ScanQueueProcessor> logger, IServiceScopeFactory scopeFactory)
{
_logger = logger;
_scopeFactory = scopeFactory;
}
public async ValueTask EnqueueAsync(ScanRequest request) => await _queue.Writer.WriteAsync(request);
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await foreach (var request in _queue.Reader.ReadAllAsync(stoppingToken))
{
using var scope = _scopeFactory.CreateScope();
var scanService = scope.ServiceProvider.GetRequiredService<IScanService>();
await scanService.ScanAsync(request);
}
}
}
Background tasks let your application handle long-running operations without blocking HTTP request processing.
← Previous
ASPNET Localization — Localizing Content and Views in .NET
Next →
ASPNET Hosting Environments — Managing Environments in ASP.NET Core
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro