Health checks provide availability and state information about an app. Health checks are often exposed as HTTP endpoints, but can also be used internally by the app to write logs or perform other tasks based on the current health. Health checks are typically used in combination with an external monitoring service or container orchestrator to check the status of an app.
Readiness (/health) - “Am I ready to receive traffic?” Checks that dependencies are connected, initialization is complete, and the service can handle requests. A failing readiness check means “don’t send me traffic yet.”
Liveness (/alive) - “Am I still running?” Checks that the process hasn’t deadlocked or crashed. A failing liveness check means “restart me.”
Aspire exposes two default health check HTTP endpoints in Development environments when the AddServiceDefaults and MapDefaultEndpoints methods are called from the Program.cs file:
The /health endpoint indicates if the app is running normally where it’s ready to receive requests. All health checks must pass for app to be considered ready to accept traffic after starting.
HTTP
GET /health
The /health endpoint returns an HTTP status code 200 and a text/plain value of Healthy when the app is healthy.
The /alive indicates if an app is running or has crashed and must be restarted. Only health checks tagged with the live tag must pass for app to be considered alive.
HTTP
GET /alive
The /alive endpoint returns an HTTP status code 200 and a text/plain value of Healthy when the app is alive.
The AddServiceDefaults and MapDefaultEndpoints methods also apply various configurations to your app beyond just health checks, such as OpenTelemetry and service discovery configurations.
In non-development environments, the /health and /alive endpoints are disabled by default. If you need to enable them, its recommended to protect these endpoints with various routing features, such as host filtering and/or authorization. For more information, see Health checks in ASP.NET Core.
Additionally, it may be advantageous to configure request timeouts and output caching for these endpoints to prevent abuse or denial-of-service attacks. To do so, consider the following modified AddDefaultHealthChecks method:
// are automatically available when the Express app starts
app.listen(8080,()=>{
console.log('Server running on port 8080');
});
The preceding code:
Groups the health check endpoints under the / path.
Caches the output and specifies a request time with the corresponding HealthChecks policy.
In addition to the updated AddDefaultHealthChecks and MapDefaultEndpoints methods, you must also add the corresponding services for both request timeouts and output caching.
In the appropriate consuming app’s entry point (usually the Program.cs file), add the following code:
Aspire integrations can also register additional health checks for your app. These health checks contribute to the returned status of the /health and /alive endpoints. For example, the Aspire PostgreSQL integration automatically adds a health check to verify the following conditions:
A database connection could be established.
A database query could be executed successfully.
If either of these operations fail, the corresponding health check also fails.
You can disable health checks for a given integration using one of the available configuration options. Aspire integrations support Microsoft.Extensions.Configurations to apply settings through config files such as appsettings.json:
JSON — appsettings.json
{
"Aspire":{
"Npgsql":{
"DisableHealthChecks":true
}
}
}
You can also use an inline delegate to configure health checks:
AppHost resource health checks are different from the health check endpoints described earlier. These health checks are configured in the AppHost project and determine the readiness of resources from the orchestrator’s perspective. They’re particularly important for controlling when dependent resources start via the WaitFor functionality and are displayed in the Aspire dashboard.
When a resource has health checks configured, the AppHost uses them to determine if the resource is ready before starting dependent resources. If no health checks are registered for a resource, the AppHost waits for the resource to be in the Running state.
You can create custom health checks for more complex readiness scenarios. Start by defining the health check in the AppHost’s service collection, then associate it with resources:
.WaitFor(pg);// Waits for both the Postgres container to be running
// AND the custom "mycheck" health check to be healthy
The AddCheck method registers the health check, and WithHealthCheck associates it with specific resources. For more details about creating and registering custom health checks, see Create health checks.
Resource health check status is displayed in the Aspire dashboard, providing real-time visibility into resource readiness. When resources are waiting for health checks to pass, the dashboard shows the current status and any failure details.