Adding health checks to OpenAPI using the new document transformers
In all previous versions of ASP.NET Core (before .NET 9), integrating health checks into your OpenAPI documentation (such as Swagger) was surprisingly challenging – especially when using the MapHealthChecks
method. The normal workaround was to create an entire health checks
controller just so the Healthchecks
endpoint would be mapped to OpenAPI.
With the introduction of the Microsoft.AspNetCore.OpenApi
namespace in .NET 9, we now have a more elegant solution. This built-in functionality provides Transformers – powerful new tools that allow us to modify the OpenAPI document directly via the Program.cs
file, integrating our HealthCheck
without additional controllers.
Getting ready
You can pick up where we left off in the preceding recipe, or clone the starter project here: