I'm trying to use the .NET Core 2.2 Health Checks.
In ConfigureServices
I registered my class that implements the Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck
interface.
But when I execute the UseHealthChecks
extension method inside the Configure
method, it throws an error:
public void Configure(IApplicationBuilder app)
{
app.UseHealthChecks("/hc"); // <-- Error in this line
// ...
System.InvalidOperationException: 'Unable to resolve service for type 'Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheckService' while attempting to activate 'Microsoft.AspNetCore.Diagnostics.HealthChecks.HealthCheckMiddleware'
In my case the health checks UI itself not launching and crashing the .net core 3.1 web API application.
Error Message: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: HealthChecks.UI.Core.Notifications.IHealthCheckFailureNotifier Lifetime: Scoped ImplementationType: HealthChecks.UI.Core.Notifications.WebHookFailureNotifier': Unable to resolve service for type 'HealthChecks.UI.Core.Data.HealthChecksDb' while attempting to activate 'HealthChecks.UI.Core.Notifications.WebHookFailureNotifier'.)
Fix: Add any UI storage provider . In my case I have choosen AddInMemoryStorage()
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
...
services.AddHealthChecks()
.AddDbContextCheck<PollDbContext>() //nuget: Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore
.AddApplicationInsightsPublisher(); //nuget: AspNetCore.HealthChecks.Publisher.ApplicationInsights
services.AddHealthChecksUI() //nuget: AspNetCore.HealthChecks.UI
.AddInMemoryStorage(); //nuget: AspNetCore.HealthChecks.UI.InMemory.Storage
...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseHealthChecks("/healthcheck", new HealthCheckOptions
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse //nuget: AspNetCore.HealthChecks.UI.Client
});
//nuget: AspNetCore.HealthChecks.UI
app.UseHealthChecksUI(options =>
{
options.UIPath = "/healthchecks-ui";
options.ApiPath = "/health-ui-api";
});
...
}
appsettings.json
"HealthChecks-UI": {
"DisableMigrations": true,
"HealthChecks": [
{
"Name": "PollManager",
"Uri": "/healthcheck"
}
],
"Webhooks": [
{
"Name": "",
"Uri": "",
"Payload": "",
"RestoredPayload": ""
}
],
"EvaluationTimeOnSeconds": 10,
"MinimumSecondsBetweenFailureNotifications": 60,
"MaximumExecutionHistoriesPerEndpoint": 15
}
You'll have to configure the health check infrastructure services via the AddHealthChecks()
extension method. For example:
public void ConfigureServices(IServiceCollection services)
{
services.AddHealthChecks();
}
Also see the documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With