skip to content

In an Aspire ServiceDefaults project, why does MapDefaultEndpoints map /health and /alive only in Development, and what should change before production?

level: seniorimportance: should knowfreq 34%

answer

  1. a guard you own in Extensions.cs
  2. app.Environment.IsDevelopment()
  3. /health: every check; /alive: tag live
  4. self check is always Healthy
  5. RequireHost or a separate port

basics

~20 s

The template wraps both MapHealthChecks calls in IsDevelopment() because public health routes have security implications. In Production they return 404, so probes that expect them fail until you map them deliberately, limited to an internal port or host.

solid answer

~40 s

In the Aspire 13.6 template, `MapDefaultEndpoints` calls `app.MapHealthChecks("/health")` and `app.MapHealthChecks("/alive", ...)` with a `Predicate` that keeps checks tagged `live`, but only inside `if (app.Environment.IsDevelopment())`, citing security implications. `/health` runs every registered check, including the ones client integrations add for databases and caches; `/alive` runs only `self`. Tracing filters both paths out. In Production the routes do not exist, so a platform probe or the AppHost's `WithHttpHealthCheck("/health")` gets 404. The aspire.dev page's prose omits the guard, while its later sample and the template have it. Before production, decide what each orchestrator probes and edit `Extensions.cs`: map the routes in every environment but restrict them, for example with `RequireHost` on an internal port, and keep the default status-only response.

code

csharp · 16 lines
csharp
public static WebApplication MapDefaultEndpoints(this WebApplication app)
{
    var health = app.MapHealthChecks(HealthEndpointPath);
    var alive = app.MapHealthChecks(AlivenessEndpointPath, new HealthCheckOptions
    {
        Predicate = r => r.Tags.Contains("live")
    });

    if (!app.Environment.IsDevelopment())
    {
        health.RequireHost("*:8081");
        alive.RequireHost("*:8081");
    }

    return app;
}

go deeper

for a junior

Recall that MapDefaultEndpoints maps /health and /alive, that /alive runs only the live-tagged self check, and that the template maps them only in Development.

for a middle

Explain the Predicate on /alive, which checks client integrations add to /health, and why tracing filters both paths.

for a senior

Show that you catch the production 404 before go-live, restrict the routes to an internal host or port, and match each probe to the right route.

for a principal

Decide a platform-wide health contract for every service, including ports, paths and response detail, and who reviews changes to it.

## What the template maps `MapDefaultEndpoints(this WebApplication app)` in the Aspire 13.6 `aspire-servicedefaults` template does three things: 1. Checks `app.Environment.IsDevelopment()`; outside Development it maps nothing and returns. 2. Maps `/health` with `app.MapHealthChecks("/health")`, which runs **every** registered health check. 3. Maps `/alive` with a `HealthCheckOptions.Predicate` of `r => r.Tags.Contains("live")`, which runs only checks tagged `live`. `AddDefaultHealthChecks()` registered exactly one check, `self`, tagged `live` and always Healthy. Client integrations add more, such as a PostgreSQL check from `AddNpgsqlDataSource`, without that tag. So in a running service of the insurance-quoting platform, `/alive` answers whether the process responds, and `/health` answers whether it and its dependencies are usable. `ConfigureOpenTelemetry` filters both paths out of ASP.NET Core tracing so frequent probes do not flood traces. ## Why the guard exists The comment in the template says that adding health endpoints in non-development environments has security implications. Concretely: - `/health` runs every check on each call, including real round trips to databases and caches, so a public route lets anyone drive load on dependencies. - Health responses reveal topology when a custom response writer prints check names, exceptions or durations. - A public route is attack surface that has nothing to do with the product. The guard keeps the safe choice the default: during `aspire run` the environment is Development and everything works, including the AppHost's `WithHttpHealthCheck("/health")` that `WaitFor` relies on. ## The documentation trap The aspire.dev ServiceDefaults page describes `MapDefaultEndpoints` as mapping `/health` and `/alive` without mentioning the environment, though its own code sample further down shows the guard. Teams that read only the prose expect the routes in production and wire orchestrator probes to them. The template code is what runs. ## What breaks in production | Caller | Expects | Gets in Production with the template unchanged | |---|---|---| | liveness probe on `/alive` | 200 | 404, so the platform may restart a healthy instance | | readiness probe on `/health` | 200 or 503 | 404, so the instance never receives traffic or is marked failed | | load balancer health check | 200 | 404, so the target is drained | Whether a 404 is treated as failure depends on the platform, which is a deployment concern; the Aspire point is that nothing answers. ## What to change before production Because `Extensions.cs` is your code, the fix is an edit, not a setting: - **Map the routes in every environment, but restrict them.** `RequireHost("*:8081")` on both endpoints limits them to an internal port that only the platform reaches; the service also listens on that port, configured outside `Extensions.cs`. - **Keep the response minimal.** The default writer returns only the overall status text; do not add a writer that serializes check details on a reachable route. - **Decide what each probe means.** Pointing a liveness probe at `/health` would restart instances whenever a database blips; `/alive` exists to avoid that. - **Keep the tracing filter in step.** If you rename the paths, update the constants the filter uses, or probes reappear as traces. ## Interview angle Interviewers use this to see whether a candidate has read the generated code. "It maps `/health`" is the docs answer; "it maps `/health` only in Development, so production probes 404 until you change it" is the answer of someone who has deployed an Aspire service. ## A safe production checklist Before the first deployment of the five quoting services, a reviewer of the shared `Extensions.cs` should be able to answer yes to each line: 1. Every route a platform probe calls exists outside Development. 2. Those routes are reachable only from inside the platform, by host, port or network policy. 3. Liveness uses the `live`-tagged route and readiness the full one. 4. Responses carry only the status, not check names or exception text. 5. The tracing filter still matches the mapped paths. Because one project serves every service, these answers are made once and reviewed once, which is the strongest argument for keeping health exposure in ServiceDefaults rather than in each `Program.cs`.

  • Why should a liveness probe target /alive rather than /health?
    `/alive` runs only checks tagged `live`, by default the `self` check, so it fails only when the process itself stops answering. `/health` also runs dependency checks; using it for liveness turns a short database outage into restarts of every instance, which makes recovery slower.
  • Does the AppHost's WithHttpHealthCheck("/health") still work after you add RequireHost("*:8081")?
    Only if the AppHost probes that port. In the edited example the restriction applies outside Development, so `aspire run` is unaffected. If you restrict in every environment, give the project an endpoint for the management port and point `WithHttpHealthCheck` at it with its `endpointName` argument.

  • MapDefaultEndpoints exposes /health and /alive in every environment.
  • /alive runs the database checks, so a slow database fails the liveness probe.
  • The tracing filter on the health paths also hides the endpoints from callers.
  • The AppHost's WithHttpHealthCheck adds the /health endpoint to the service.
  • Exposing health routes is a setting in appsettings.json, not a code change.

Assumes the Aspire 13.6 aspire-servicedefaults template on ASP.NET Core 10; the IsDevelopment guard is generated code you own, so a solution created from another template version may differ.