Now it is time to see a test application. For this I create a new .NET Core 3.1 ASP.NET application,add Xabaril ( to see the end result ) and add reference to my project. The Startup is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using HCVersion;
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace NetCore31HC
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
string name = Assembly.GetExecutingAssembly().GetName().Name;
services.AddControllers();
services.AddHealthChecks()
.AddCheck<HealthCheckVersion>(name);
services
.AddHealthChecksUI(setup =>
{
setup.AddHealthCheckEndpoint("All",$"/hc");
})
.AddInMemoryStorage()
;
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHealthChecks("/hc",new HealthCheckOptions()
{
Predicate= _=> true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
endpoints.MapHealthChecksUI();
});
}
}
}
And so I figure out the fact that I should not hardcode the name of the HealthCheck,but to put dinamically. So I change also the readme file to acknowledge that.
Code at https://github.com/ignatandrei/HealthCheckVersion/releases/tag/testApp
Leave a Reply