Search code examples
c#asp.net-core

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'


Solution

  • 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.