HealthyCron HealthyCron
Dashboard
Guides / .NET & C# Integration

.NET & C# Integration

HealthyCron fits naturally into modern .NET applications. Here are the most common ways to integrate the pinging API inside your background workers and services.

Using IHostedService (ASP.NET Core Worker)

If you're building a .NET Worker Service or running an IHostedService in ASP.NET Core, use an HttpClient to ping after the work loop executes.

C# (.NET 8.0)
public class DataExportWorker : BackgroundService
{
    private readonly HttpClient _httpClient;

    public DataExportWorker(IHttpClientFactory httpClientFactory)
    {
        _httpClient = httpClientFactory.CreateClient();
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        // Run every day, measuring success via PeriodicTimer
        using var timer = new PeriodicTimer(TimeSpan.FromDays(1));

        while (await timer.WaitForNextTickAsync(stoppingToken))
        {
            try 
            {
                // 1. Optional Start Ping (measures duration)
                await _httpClient.GetAsync("https://healthycron.io/ping/{uuid}/start", stoppingToken);

                // Do heavy work
                await ExportDataToWarehouseAsync(stoppingToken);

                // 2. Success Ping
                await _httpClient.GetAsync("https://healthycron.io/ping/{uuid}", stoppingToken);
            }
            catch (Exception ex)
            {
                // 3. Failure Ping (bypasses grace period for instant alert)
                await _httpClient.GetAsync("https://healthycron.io/ping/{uuid}/fail", stoppingToken);
            }
        }
    }
}

AWS Lambda (Scheduled CloudWatch Events)

If invoking a Lambda via Amazon EventBridge on a cron schedule, use the standard HttpClient to ping at the end of the handler.

C# LAMBDA
public async Task FunctionHandler(ILambdaContext context)
{
    try 
    {
        await ProcessQueueAsync();
        await _httpClient.GetAsync("https://healthycron.io/ping/{uuid}");
    }
    catch (Exception)
    {
        await _httpClient.GetAsync("https://healthycron.io/ping/{uuid}/fail");
        throw;
    }
}
The Number One Mistake
Never swallow exceptions without pinging the fail endpoint. If your script catches an error, logs it to DataDog, and exits successfully, HealthyCron will NOT alert you because to us, the job never arrived (which is Late wait time), rather than an explicit Failure. Always call /ping/{uuid}/fail in your catch block before re-throwing or exiting!