Using custom health checks with Asp.Net Core 2.2

I almost burst with cries of joy when I saw the release of .Net Core 2.2 with the much needed built in and standardized health checks. The preview was great and with each version until the release new blog posts emerged eager to show to the world the new wonder. But as I do like my frameworks fully baked and released I waited patiently for the real deal.
TL;DR Implement your own service from IHealthCheck and inject it with whatever you want. It just works!

Reading the instructions...

The best article I found for the new healthcheck API was this one based on preview code and the best code samples where of course the official samples. However by looking at them something was missing. So I went on a quest to achieve what suited my needs. And my needs are simple. I need to be able to inject some testing code to my custom health check module.

Healthcheck for your .Net Core2.2 Services

On to the code

After I practiced a little bit with the concept of tags I just added a health checking service to the standard DI container as such
Oddly enough it was as simple as that. It worked right out of the box. And you can still use tags and different kinds of routing paths for liveliness and readiness checks. This way the actual health check and all the code that comes with it, database checks, broker connection checks, (put you check here) is decoupled from the actual IHealthCheck interface implementation. This way you can break your individual external resources checks on different classes, DI them to the interface implementation and keep your concerns separated and your code clean and concise. 


And of course for your coding pleasure you can download the code from here and navigate to "http://localhost:5000/health". Press F5 a few times and the call will return sometimes "Healthy" and some times "Unhealthy"

Enjoy!

Comments

Popular posts from this blog

Visual Studio Code setup I use for C# development

Tests code coverage in Visual Studio Code with C# and .Net Core

Running an Asp.Net Core 2.* Web API as a Windows Service