Monitor ASP.NET Worker Services with CronRabbit
ASP.NETThe Problem
.NET Worker Services run long-lived background tasks. If the service crashes or a task loop hangs, there's no built-in external notification.
The Solution
Add CronRabbit pings inside your Worker's ExecuteAsync loop to verify each iteration completes.
Integration
Inject IHttpClientFactory into your Worker and ping CronRabbit after each successful iteration.
Code Examples
Worker Service with monitoring
C#public class Worker : BackgroundService
{
private readonly HttpClient _http;
private const string Ping = "https://ping.cronrabbit.com/your-id";
public Worker(IHttpClientFactory factory) =>
_http = factory.CreateClient();
protected override async Task ExecuteAsync(CancellationToken ct)
{
while (!ct.IsCancellationRequested)
{
try
{
await ProcessBatch();
await _http.GetAsync(Ping, ct);
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
await _http.GetAsync($"{Ping}/fail");
}
await Task.Delay(TimeSpan.FromMinutes(30), ct);
}
}
}