7

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")
Zach Hutchins
  • 801
  • 1
  • 12
  • 16

1 Answers1

13

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()

sriram hegde
  • 2,301
  • 5
  • 29
  • 43
Stack Undefined
  • 1,050
  • 1
  • 14
  • 23