Atvik
Mar 17, 9 PM - Mar 21, 10 AM
Taktu þátt í fundarröðinni til að byggja upp skalanlegar gervigreindarlausnir byggðar á raunverulegum notkunartilvikum með öðrum forriturum og sérfræðingum.
Nýskrá núnaÞessi vafri er ekki lengur studdur.
Uppfærðu í Microsoft Edge til að nýta þér nýjustu eiginleika, öryggisuppfærslur og tæknilega aðstoð.
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. The data reported by health checks can be used for various scenarios:
.NET 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.
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.
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:
public static IHostApplicationBuilder AddDefaultHealthChecks(this IHostApplicationBuilder builder)
{
builder.Services.AddRequestTimeouts(
configure: static timeouts =>
timeouts.AddPolicy("HealthChecks", TimeSpan.FromSeconds(5)));
builder.Services.AddOutputCache(
configureOptions: static caching =>
caching.AddPolicy("HealthChecks",
build: static policy => policy.Expire(TimeSpan.FromSeconds(10))));
builder.Services.AddHealthChecks()
// Add a default liveness check to ensure app is responsive
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);
return builder;
}
The preceding code:
HealthChecks
.HealthChecks
.Now consider the updated MapDefaultEndpoints
method:
public static WebApplication MapDefaultEndpoints(this WebApplication app)
{
var healthChecks = app.MapGroup("");
healthChecks
.CacheOutput("HealthChecks")
.WithRequestTimeout("HealthChecks");
// All health checks must pass for app to be
// considered ready to accept traffic after starting
healthChecks.MapHealthChecks("/health");
// Only health checks tagged with the "live" tag
// must pass for app to be considered alive
healthChecks.MapHealthChecks("/alive", new()
{
Predicate = static r => r.Tags.Contains("live")
});
return app;
}
The preceding code:
/
path.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:
// Wherever your services are being registered.
// Before the call to Build().
builder.Services.AddRequestTimeouts();
builder.Services.AddOutputCache();
var app = builder.Build();
// Wherever your app has been built, before the call to Run().
app.UseRequestTimeouts();
app.UseOutputCache();
app.Run();
For more information, see Request timeouts middleware in ASP.NET Core and Output caching middleware in ASP.NET Core.
.NET 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 .NET Aspire PostgreSQL integration automatically adds a health check to verify the following conditions:
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. .NET Aspire integrations support Microsoft.Extensions.Configurations to apply settings through config files such as appsettings.json:
{
"Aspire": {
"Npgsql": {
"DisableHealthChecks": true,
}
}
}
You can also use an inline delegate to configure health checks:
builder.AddNpgsqlDbContext<MyDbContext>(
"postgresdb",
static settings => settings.DisableHealthChecks = true);
.NET Aspire umsögn
.NET Aspire er opið verkefni. Veldu tengil til að gefa umsögn:
Atvik
Mar 17, 9 PM - Mar 21, 10 AM
Taktu þátt í fundarröðinni til að byggja upp skalanlegar gervigreindarlausnir byggðar á raunverulegum notkunartilvikum með öðrum forriturum og sérfræðingum.
Nýskrá núnaÞjálfun
Eining
Introduction to .NET Aspire - Training
In this module, you learn about cloud-native applications and how the .NET Aspire stack makes it easier to develop microservices and connect them with each other and with supporting services.
Skjöl
.NET Aspire service defaults - .NET Aspire
Learn about the .NET Aspire service defaults project.
.NET Aspire service discovery - .NET Aspire
Understand essential service discovery concepts in .NET Aspire.
.NET Aspire and launch profiles - .NET Aspire
Learn how .NET Aspire integrates with .NET launch profiles.