Search code examples
c#asp.net-corehealth-check

Usehealthchecks vs Maphealthchecks


I've looked around from place to place but I am assuming one is an older way of doing things, but is there a more deeper difference to adding Health Check Endpoints to the middleware?

In Startup.Configure()..

app.UseEndpoints(endpoints =>
{
    endpoints.MapHealthChecks("/health");
});

app.UseHealthChecks("/health")

Solution

  • UseHealthChecks and MapHealthChecks have subtle differences, UseHealthChecks allows you to capture any endpoint on a port when using null or empty PathString, MapHealthChecks does not allow this, using null throws an exception, and an empty string is just the equivalent of /.

    They both use the same middleware HealthCheckMiddleware behind the scene. The MapHealthChecks is an extension method on IEndpointRouteBuilder, whereas UseHealthChecks is an extension method on IApplicationBuilder.

    Here is a reference to the source for reference.

    https://github.com/dotnet/aspnetcore/tree/main/src/Middleware/HealthChecks/src/Builder

    If you look at the source you will see that UseHealthChecks uses MapWhen() where MapHealthChecks uses Map()