I am experiencing some issues with the AspNetCore.HealthChecks.UI package. I made the necessary settings in the Startup class of my API project, but when I access the endpoint to view the HealthCheck interface, only a JSON is displayed.
My project is on version 3.1 of .NET Core, in which I installed the following nuget packages:
- AspNetCore.HealthChecks.UI v3.1.3
- AspNetCore.HealthChecks.UI.Client v3.1.2
- AspNetCore.HealthChecks.UI.InMemory.Storage v3.1.2
- Microsoft.Extensions.Diagnostics.HealthChecks v5.0.9
Below is the extension method in which I use the HealthCheck settings:
public static IServiceCollection AddHealthCheckConfiguration(
this IServiceCollection services,
IConfiguration configuration)
{
string mongoConnectionString = configuration.GetSection("MongoSettings").GetSection("Connection").Value;
string mongoDatabaseName = configuration.GetSection("MongoSettings").GetSection("DatabaseName").Value;
string redisConnectionString = configuration.GetConnectionString("Redis");
services
.AddHealthChecks()
.AddRedis(redisConnectionString)
.AddMongoDb(mongoConnectionString, mongoDatabaseName: mongoDatabaseName);
services.AddHealthChecksUI(opt =>
{
opt.SetEvaluationTimeInSeconds(15);
opt.MaximumHistoryEntriesPerEndpoint(60);
opt.SetApiMaxActiveRequests(1);
opt.AddHealthCheckEndpoint("default api", "/api-health");
})
.AddInMemoryStorage();
return services;
}
And in another extension method I add the necessary settings for displaying the interface:
public static IApplicationBuilder UseMvcConfiguration(this IApplicationBuilder app)
{
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHealthChecks("/api-health", new HealthCheckOptions
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
endpoints.MapHealthChecksUI();
});
return app;
}
Finally, my Startup class looks like this:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddApiResponseCompressionConfig();
services.AddApiCachingConfig(Configuration);
services.AddHateoasConfig();
services.AddMongoDbConfig();
services.AddAutoMapper(typeof(Startup));
services.AddWebApiConfig();
services.AddHealthCheckConfiguration(Configuration);
services.ResolveGeneralDependencies();
services.ResolveRepositories();
services.ResolveApplicationServices();
services.AddSwaggerConfig();
}
public void Configure(
IApplicationBuilder app,
IWebHostEnvironment env,
IApiVersionDescriptionProvider provider)
{
app.UseResponseCompression();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMiddleware<ExceptionMiddleware>();
app.UseHealthChecks("/healthcheck");
app.UseMvcConfiguration();
app.UseSwaggerConfig(provider);
}
}
I searched for similar errors here on StackOverflow, but none of the answers I found solved my problem.