Background services look harmless… until production.
Common mistake
-
Long-running loops
-
No cancellation handling
-
Blocking delays
Correct pattern
while (!stoppingToken.IsCancellationRequested)
{
await DoWorkAsync(stoppingToken);
await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
}
Why
Graceful shutdowns save data and prevent zombie processes.
