Monitor ASP.NET Background Services with CronRabbit
C#The Problem
ASP.NET Core BackgroundService runs inside the web host process. If it throws an unhandled exception, the service stops but the web app keeps running — masking the failure.
The Solution
Add CronRabbit pings inside your BackgroundService's ExecuteAsync loop to verify each iteration completes.
Pattern
In your ExecuteAsync while-loop, send a ping after each successful iteration. The monitor's schedule matches your loop interval.
Code Examples
BackgroundService with monitoring
C#public class CleanupService : BackgroundService
{
private readonly HttpClient _http;
private const string Ping = "https://ping.cronrabbit.com/your-id";
public CleanupService(IHttpClientFactory factory)
{
_http = factory.CreateClient();
_http.Timeout = TimeSpan.FromSeconds(10);
}
protected override async Task ExecuteAsync(CancellationToken ct)
{
while (!ct.IsCancellationRequested)
{
await _http.GetAsync($"{Ping}/start", ct);
try
{
await CleanupExpiredData();
await _http.GetAsync(Ping, ct);
}
catch (Exception ex)
{
await _http.GetAsync($"{Ping}/fail");
}
await Task.Delay(TimeSpan.FromHours(1), ct);
}
}
}