Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core - Error trying to use HealthChecks

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'

like image 892
lmcarreiro Avatar asked Jan 09 '19 13:01

lmcarreiro


Video Answer


2 Answers

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
    }
like image 149
hpsanampudi Avatar answered Oct 16 '22 19:10

hpsanampudi


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.

like image 43
Henk Mollema Avatar answered Oct 16 '22 18:10

Henk Mollema