Usehealthchecks vs Maphealthchecks
Asked Answered
G

1

14

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")
Geibel answered 25/5, 2022 at 22:8 Comment(0)
F
21

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

Florenceflorencia answered 25/5, 2022 at 23:37 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.