Looks fine locally.
Stops running in prod.
Root cause
Unhandled exceptions inside ExecuteAsync.
What actually happens
The host kills the service quietly.
Correct pattern
protected override async Task ExecuteAsync(CancellationToken ct)
{
while (!ct.IsCancellationRequested)
{
try
{
await DoWork(ct);
}
catch (Exception ex)
{
logger.LogError(ex, "Background task failed");
await Task.Delay(5000, ct);
}
}
}
Cause → Effect
Unhandled exception → service stops → no alerts → ghost bugs.
