ASP.NET Core / Basic: HTTP and Application Composition / Lesson 7
Health endpoints and service readiness
Expose a bounded health endpoint and distinguish process liveness from readiness to serve traffic.
On this page
Lesson goals
- Explain liveness and readiness as distinct operational questions.
- Register and map an ASP.NET Core health check.
- Test probe behavior and protect sensitive diagnostics.
Define what each probe means
A liveness probe asks whether a process should be restarted; a readiness probe asks whether it should receive traffic. The exact orchestrator behavior is deployment-specific. A database outage may make an application temporarily unready without proving that restarting every instance will repair the database. Define each probe’s consumers and failure policy before connecting it to automation, and avoid expensive checks that amplify an outage.
Register an application check
The first fragment registers health-check services and maps an endpoint. An endpoint with no application-specific checks demonstrates host response but does not prove database, queue or business-operation health. Add checks for dependencies only when their result is meaningful for the intended probe. Set timeouts and avoid exposing connection strings, exception messages or other secrets through public diagnostics.
builder.Services.AddHealthChecks();
var app = builder.Build();
app.MapHealthChecks("/health");
app.Run();
This fragment assumes an ASP.NET Core application builder and does not add a dependency check.
Separate readiness from liveness
The second fragment shows two endpoints selecting checks by tag. It assumes a separately registered readiness check tagged ready; an empty predicate makes the live endpoint report the host pipeline rather than dependency status. A check should report the condition it actually measured, with bounded time and a clear failure result. Test both paths when dependencies are healthy and when a controlled dependency fails.
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
app.MapHealthChecks("/live", new HealthCheckOptions { Predicate = _ => false });
app.MapHealthChecks("/ready", new HealthCheckOptions { Predicate = check => check.Tags.Contains("ready") });
Register application-specific checks with the ready tag. The code shows selection, not a database health implementation.
Control exposure and load
Health URLs are operational interfaces. Decide whether they are restricted to infrastructure, what response details they reveal and how frequently they are called. A public endpoint should not enumerate internal hosts or credentials. Frequent expensive checks can add load to a struggling dependency; use an appropriate timeout and monitoring interval, and distinguish a lightweight probe from deeper synthetic transactions.
Verify the deployment contract
In a disposable host, request each endpoint and assert status and safe response content for a healthy and a controlled unhealthy case. Then validate the actual deployment’s probe path, network permissions and automation behavior without deliberately disrupting production. Include a case in which a dependency recovers. An in-process test does not establish that a load balancer or orchestrator has configured the same endpoint.
Common errors and fixes
A database outage restarts every instance
- Why it happens
- A dependency readiness check was used as an unconditional liveness signal.
- How to fix it
- Separate the probe purposes and recovery policy.
- Prevent it next time
- Test dependency failure without assuming a process restart repairs it.
A green endpoint is assumed to prove all dependencies
- Why it happens
- Only host response was checked.
- How to fix it
- Register bounded checks for the intended readiness contract.
- Prevent it next time
- Test a controlled dependency failure.
Health details expose credentials
- Why it happens
- An internal diagnostic response was made public.
- How to fix it
- Restrict access and return only safe information.
- Prevent it next time
- Review output and infrastructure exposure in deployment tests.
2 practice exercises
1. The database is down but the process responds. Should liveness necessarily fail?
Show worked solution
No. Decide whether restarting the process addresses the failure. The instance may be unready while remaining live; test the orchestrator policy and database recovery in an isolated environment.
2. A health endpoint returns 200 with no registered checks. What does it prove?
Show worked solution
It demonstrates that the endpoint pipeline answered under those conditions. It does not prove database or external service health. Add and test only checks that support the intended operational decision.
Key takeaways
- Liveness and readiness drive different operational decisions.
- A health result is evidence only for checks actually performed.