---
title: "Ping CronMissed from C# and .NET"
url: "https://cronmissed.com/snippets/csharp.md"
canonical_url: "https://cronmissed.com/snippets/csharp"
last_updated: "2026-10-22"
type: "how-to"
summary: "A C# helper using HttpClient that reports start, success and failure to CronMissed with a timeout and the tail of the exception as the body."
related: ["https://cronmissed.com/snippets.md", "https://cronmissed.com/guides/windows-task-scheduler.md", "https://cronmissed.com/snippets/powershell.md", "https://cronmissed.com/guides/kubernetes-cronjob.md", "https://cronmissed.com/snippets/shell.md"]
---

# Ping CronMissed from C# and .NET

> **For AI agents:** the complete index of CronMissed pages is [llms.txt](https://cronmissed.com/llms.txt). Every page is also served as markdown at its URL plus .md.

A C# helper using HttpClient that reports start, success and failure to CronMissed with a timeout and the tail of the exception as the body.

In .NET one shared HttpClient does the job. This helper sends the start signal, runs your task, and reports success or failure with the end of the exception text, using a ten second timeout and swallowing its own errors.

## What should a C# job send?

- A request to `/start` before the work, so CronMissed can time the run and notice a job that never finishes.
- A request to the plain URL when the work succeeded, or to `/fail` when it did not. `/<exit status>` works too: 0 is a success and any other number is a failure.
- The end of the output in the request body, up to 100,000 bytes, so the alert shows the error without a trip to the server.

## What is the helper?

```
using System.Net.Http;

public static class CronMissed
{
    private static readonly HttpClient Http = new() { Timeout = TimeSpan.FromSeconds(10) };

    public static async Task PingAsync(string url, string suffix = "", string? body = null)
    {
        try
        {
            using var content = body is null ? null : new StringContent(body);
            using var response = body is null
                ? await Http.GetAsync(url + suffix)
                : await Http.PostAsync(url + suffix, content);
        }
        catch (Exception)
        {
            // a lost ping must never break the job
        }
    }

    public static async Task RunAsync(string url, Func<Task> job)
    {
        await PingAsync(url, "/start");
        try
        {
            await job();
        }
        catch (Exception e)
        {
            var text = e.ToString();
            await PingAsync(url, "/fail", text.Length > 100000 ? text[^100000..] : text);
            throw;
        }
        await PingAsync(url);
    }
}
```

Keep the ping URL in an environment variable or a secret store, never in a public repository: the URL is the check's only credential.

## Which hosts does this fit?

A console app started by [Windows Task Scheduler](https://cronmissed.com/guides/windows-task-scheduler.md), a hosted service on a timer, or a container started by [Kubernetes](https://cronmissed.com/guides/kubernetes-cronjob.md). For a long-running worker, call PingAsync after each pass.

## Questions

### Why a static HttpClient?

Creating a client per request can exhaust sockets in a busy process. One shared instance with a timeout is the usual pattern.

### Does this work in a background service?

Yes. Call RunAsync around each iteration or PingAsync at the end of each pass; give the check a period that matches the loop.

## Related pages

- [Windows Task Scheduler](https://cronmissed.com/guides/windows-task-scheduler.md)
- [PowerShell](https://cronmissed.com/snippets/powershell.md)
- [Kubernetes CronJobs](https://cronmissed.com/guides/kubernetes-cronjob.md)
- [Shell and curl](https://cronmissed.com/snippets/shell.md)

## Related

- [Ping snippets for 12 languages and tools](https://cronmissed.com/snippets.md)
- [Monitor Windows Task Scheduler tasks](https://cronmissed.com/guides/windows-task-scheduler.md)
- [Ping CronMissed from PowerShell](https://cronmissed.com/snippets/powershell.md)
- [Kubernetes CronJob schedule: monitor every run](https://cronmissed.com/guides/kubernetes-cronjob.md)
- [Ping CronMissed from a shell script with curl](https://cronmissed.com/snippets/shell.md)
