Getting weird warning.
AsP0014: Suggest using top level route registrations
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.SetupHealthChecksEndpoints();
});
My HealthCheck is 'Healthy' whereas
HealthCheckUI is 'UnHealthy' Error Endpoint.
Pleae help to resolve.

Tuhin PaulRank #7 · 71.9K reputationPosted Jun 8, 2024, 1:05 AM EDT
Are you're registering routes using
app.UseEndpoints .In minimal APIs (introduced in ASP.NET Core 6+), it's recommended to register routes directly at the top level of the application, like this:
app.MapControllers(); app.SetupHealthChecksEndpoints()app.UseEndpointscall.app.MapControllers();andapp.SetupHealthChecksEndpoints();directly under theappvariable.Tuhin PaulRank #7 · 71.9K reputationPosted Jun 8, 2024, 1:07 AM EDT
Check how you can modify your code to use route registrations:
var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); builder.Services.AddHealthChecks() .AddCheck("example_health_check"); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.MapControllers(); app.MapHealthChecks("/health"); app.Run();builder.Services.AddControllers();andbuilder.Services.AddHealthChecks()are used to add the necessary services.Hemapriya67 reputationPosted Jun 10, 2024, 1:24 AM EDT
Thanks Jithu!
Unfortunately still doesn't work ! Any help please.
builder.Services.AddHealthChecksUI(setupSettings: setup =>
{
setup.AddHealthCheckEndpoint("example_health_check", "/health-ui");
}).AddInMemoryStorage();
app.MapHealthChecksUI(healthChecks => healthChecks
.UIPath = "/health-ui");
Hemapriya67 reputationPosted Jun 10, 2024, 1:28 AM EDT
Thanks Tuhin Paul!
Unfortunately still the same error. I corrected and used at the top level but still no luck.
Jithu ThomasRank #437 · 5.7K reputationPosted Jun 8, 2024, 1:44 AM EDT
1. Update Route Registrations
The new preferred method is to register routes directly on the app object. Here's how to adjust your code:
app.MapControllers(); app.MapHealthChecks("/health", new HealthCheckOptions { Predicate = check => check.Tags.Contains("SQL Connection") || check.Tags.Contains("rate check") || check.Tags.Contains("live check"), ResponseWriter = CustomResponseWriter.WriteHealthCheckResponse });2. Verify HealthCheckUI Configuration
If your health check endpoint is healthy but HealthCheckUI is reporting it as unhealthy, it could be a configuration issue. Make sure the HealthCheckUI is correctly configured to point to the health check endpoint.
var builder = WebApplication.CreateBuilder(args); // Add health checks builder.Services.AddHealthChecks() .AddCheck("example_health_check", () => HealthCheckResult.Healthy("Healthy result"), tags: new[] { "example_tag" }); // Add HealthChecksUI builder.Services.AddHealthChecksUI(setupSettings: setup => { setup.AddHealthCheckEndpoint("Example Health Check", "/health"); }).AddInMemoryStorage(); var app = builder.Build(); app.UseRouting(); app.MapHealthChecks("/health", new HealthCheckOptions { Predicate = check => check.Tags.Contains("SQL Connection") || check.Tags.Contains("rate check") || check.Tags.Contains("live check"), ResponseWriter = CustomResponseWriter.WriteHealthCheckResponse }); app.MapHealthChecksUI(); app.MapControllers(); app.Run();3. Custom Health Check Response Writer
Ensure the custom response writer is correctly implemented:
public static class CustomResponseWriter { public static Task WriteHealthCheckResponse(HttpContext context, HealthReport report) { context.Response.ContentType = "application/json"; // Determine the overall status string overallStatus = report.Entries.Any(e => e.Value.Status == HealthStatus.Unhealthy && e.Key == "live check") ? "Degraded" : report.Status.ToString(); // If any non-live check is unhealthy, set overall status to UnHealthy if (overallStatus != "Degraded" && report.Entries.Any(e => e.Value.Status == HealthStatus.Unhealthy)) { overallStatus = "UnHealthy"; } var response = new { status = overallStatus, checks = report.Entries.Select(e => new { name = e.Key, status = e.Value.Status.ToString(), description = e.Value.Description }), duration = report.TotalDuration }; return context.Response.WriteAsync(JsonSerializer.Serialize(response)); } }Try this way, it will work....