Need background job in your ASP.NET app? BackgroundService is built-in – no Hangfire or Quartz needed.
Create Background Service:
public class EmailCleanupService : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
// Do work
await CleanupOldEmails();
// Wait 1 hour
await Task.Delay(TimeSpan.FromHours(1), stoppingToken);
}
}
private async Task CleanupOldEmails()
{
// Your cleanup logic
}
}
Register in Program.cs:
builder.Services.AddHostedService();
Starts automatically when app starts, stops gracefully on shutdown!
Use Cases: Cache refresh, log cleanup, health checks, periodic data sync





