Health checks in ASP.NET core

In this article we will understand about the health of a web application. Then we will learn about health checks for a web application and also why we need them and how to implement them in asp.net core web application

A web application or a web api generally uses database for persistence. The database operations involving complex queries take more time to complete. It is common to use caching mechanism to improve performance of the application. Most of the applications are moving towards micro-service architecture, it is often the case for an application to depend on other application or a service. Applications have started using cloud components and services extensively so it is usual for a web application to depend on azure services and components.

As shown in the below diagram, application uses or depends on many external components

When do we say that an application is healthy?

Application being up and running is not sufficient to say that an application is healthy. For the application to perform all its operations, it should be able to access all its external resources. So for the application to be called as healthy, all its external dependencies should be up and running and should be accessible to the application.

What do we mean by health checks for an application

Health checks not only involve checking whether the application is up and running but also includes to check whether all its external dependencies are up and running and they are accessible. So even when the application is up and running cannot be called healthy if it is not able to access the database, or the cloud component or any other external dependent resource.

Health checks in ASP.NET core

ASP.NET core offers health checks middleware and various libraries for checking health of different resources. The first step in health check is to see whether the app is up and running. It is also called liveness check. It is effortless to add a liveness check for an asp.net core application.

Let's add a liveness check for an asp.net core web api application.

  1. Create an asp.net core web api application.

  2. Register health check service. Add the below highlighted line of code to Program.cs file

  3. Create a health check end point. Add the below highlighted line of code to Program.cs file

  4. Build the code using the command "dotnet build"

  5. Run the code using the command "dotnet run". The application URL where the application is listening will be displayed in the log as shown below.

  6. Launch the browser and navigate to the health check end point

The health check end point returned "Healthy" saying that the application is up and running and ready to take up requests. In this article we have discussed about health check of an application. We have seen how easy it is to implement liveness check for an asp.net core web application. In the next article we will discuss about how to check if all the external dependencies of the application are up and running. Here is the link to the github repository: HealthCheckRepo