Ron Michael
Ron Michael

Reputation: 1473

ASP.NET EF Core Health Check Returns nothing but 200 status

I'm currently implementing a health check on my Identity ASP.NET Core 3.1 project using PostgreSQL and EntifyFramework Core under Docker container.

This are the nuget packages installed in my project

Here is my Startup.cs class

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<IdentityContext>(options => options.UseNpgsql(Configuration["Identity:ConnectionString"]));

    services.AddHealthChecks()
            .AddDbContextCheck<IdentityContext>("Database");

    services.AddControllers();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseHealthChecks("/health");
    app.UseHttpsRedirection();

    app.UseRouting();

    app.UseAuthentication();
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
       endpoints.MapControllers();
    });   
}

Everything works fine, I am getting a Healthy response with 200 status code by accessing the /health endpoint until I intentionally stop the PostgreSQL container in my docker.

I am expecting to receive 503 status code with an Unhealthy response from /health but instead a got a blank response with 200 status code

Here's a snapshot of the result from postman enter image description here

Upvotes: 0

Views: 2571

Answers (1)

I think your "Database" check not called when you request url "/health". Try register HealthCheck with tag. Then define enpoint with this tag.

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<IdentityContext>(options => options.UseNpgsql(Configuration["Identity:ConnectionString"]));

    services.AddHealthChecks()
            .AddDbContextCheck<IdentityContext>("Database",tags: new[] { "live" });

    services.AddControllers();
}


public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{       
    app.UseHttpsRedirection();

    app.UseRouting();

    app.UseAuthentication();
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
       endpoints.MapControllers();

       endpoints.MapHealthChecks("/health/live", new HealthCheckOptions()
            {
                Predicate = (check) => check.Tags.Contains("live") 
            });
    });   
}

You can read about health check here

Upvotes: 0

Related Questions