Reputation: 851
I am trying to register a health in a sample .NET Core application check using the code in Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
// Adding healthcheck in ConfigureServices
services.AddHealthChecks(checks =>
{
// Custom health check which implements IHealthCheck
// Breakpoint hits here and this code is executed
checks.AddCheck<CustomHealthCheck>("Storage", new TimeSpan(0, 1, 0));
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
// This throws the exception: "Unable to find the required services..."
endpoints.MapHealthChecks("/health");
});
// I've tried this too. This also throws an exception
app.UseHealthChecks("/health");
}
However, when I run the application, I get the exception: "Unable to find the required services. Please add all the required services by calling 'IServiceCollection.AddHealthChecks' inside the call to 'ConfigureServices(...)' in the application startup code."
I'm finding this exception message confusing because I am calling AddHealthChecks in the ConfigureServices method. So I'm not sure if I've made a mistake, or if there's something else I need to do.
Any advice appreciated.
Upvotes: 0
Views: 3095
Reputation: 851
I've just revisited this. The issue is that the first uses the nuget package Microsoft.AspNetCore.HeatlhChecks
. It should use the nuget package Microsoft.AspNetCore.Diagnostics.HealthChecks
.
Confusingly, these two packages have very similar names and both define the interface IHealthCheck
, so the above code doesn't generate a compiler error message. It just throws an error.
Upvotes: 2