This repository showcases a bug in .NET Aspire.
- Create a new project using the template
.NET Aspire Starter App - In the
Webproject create a new class calledApiServiceHealthCheck - Copy the following code into
ApiServiceHealthCheck.csfile:
public class ApiServiceHealthCheck(HttpClient httpClient) : IHealthCheck
{
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
var response = await httpClient.GetAsync("http://apiservice/health", cancellationToken).ConfigureAwait(false);
return response.IsSuccessStatusCode ? HealthCheckResult.Healthy() : HealthCheckResult.Unhealthy();
}
}- In the
Webproject openProgram.csfile and add the following codebuilder.Services.AddHealthChecks().AddCheck<ApiServiceHealthCheck>("apiservice");just beforevar app = builder.Build(); - In the
AppHostproject ensure that both projects (apiserviceandwebfrontend) have.WithHttpHealthCheck("/health", 200, "http") - Run the
AppHostproject, open the dashboard and click onTracestab - You should now see a health check spam
- In the
ServiceDefaultsproject openExtensions.csfile and editAddHttpClientInstrumentation()method found inside.WithTracing(tracing => ... omitted for abbreviation ...)inConfigureOpenTelemetrymethod:
.AddHttpClientInstrumentation(options =>
{
options.FilterHttpRequestMessage = (request) =>
{
return !request.RequestUri?.PathAndQuery.Contains("/health") ?? true;
};
})- Run the
AppHostproject, open the dashboard and click onTracestab - You should no longer see any health check spam
To consider: In theory there could be a request that gets filtered out but has nothing to do with a health check.