This repository showcases a bug in .NET Aspire.
- Create a new project using the template
.NET Aspire Starter App
- In the
Web
project create a new class calledApiServiceHealthCheck
- Copy the following code into
ApiServiceHealthCheck.cs
file:
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
Web
project openProgram.cs
file and add the following codebuilder.Services.AddHealthChecks().AddCheck<ApiServiceHealthCheck>("apiservice");
just beforevar app = builder.Build();
- In the
AppHost
project ensure that both projects (apiservice
andwebfrontend
) have.WithHttpHealthCheck("/health", 200, "http")
- Run the
AppHost
project, open the dashboard and click onTraces
tab - You should now see a health check spam
- In the
ServiceDefaults
project openExtensions.cs
file and editAddHttpClientInstrumentation()
method found inside.WithTracing(tracing => ... omitted for abbreviation ...)
inConfigureOpenTelemetry
method:
.AddHttpClientInstrumentation(options =>
{
options.FilterHttpRequestMessage = (request) =>
{
return !request.RequestUri?.PathAndQuery.Contains("/health") ?? true;
};
})
- Run the
AppHost
project, open the dashboard and click onTraces
tab - 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.