Agenda: In this article, we will learn about the in-built health check middleware that .net core provides out of the box. With the help of demo by .Net development company India, we will see that how we can check the database connection health using SQL Server extension method.
- Visual Studio 2019 with .NET Core installed
- Some basic knowledge of .NET Core Middlewares
.NET Core provided out of the box middleware which can check the health of the different components of the application. These health checks are exposed with the help of an endpoint, which can be configured to monitor live application.
Health checks can be helpful to check:
- The server traffics
- The database connectivity and response
- Disk utilization
The Microsoft.AspNetCore.Diagnostics.HealthChecks package is referenced implicitly for ASP.NET Core apps.
The package AspNetCore.HealthChecks.SqlServer is used to check the health of the Sql Server Database.
There are three types of HealthStatus that are returned back to the monitoring service which uses the health check.
- Degraded - Indicates that the component was in a degraded state.
- Healthy - Indicates that the component was healthy.
- Unhealthy - Indicates that the component was unhealthy, or an unhandled exception was thrown while executing the health check.
Let’s do the demo to see all the action.
Step 1 Open Visual Studio 2019 and create a new .NET Core Web API project.
Step 2 Once the project is created, test it by pressing F5 to run it in debug mode and you should see the following output.
Step 3 Now our API template generated by Visual Studio is working fine. So let’s add the following packages to the project using Nuget package manager.
Right click on the solution explorer project name and select the nuget package library option as shown below.
Step 4 Now search the required packages and install them.
Step 5 Let’s add the Health Check service to the .NET Core pipeline. Go to Startup.cs file and make the following highlighted changes.
As shown in the screenshot above, you can see that AddHealthChecks() method is derived from the Nuget package that we installed.
Step 6 Let’s define an endpoint that can be used to check health.
As you can see in the screenshot above, we added a route with “/health” endpoint so that we can hit this endpoint for testing.
Step 7 Since we want to test the sql server database connection, we need to use extension method provided by the second nuget package that we installed. Make the changes to the code that we did in Step 5.
You can see that we used AddSqlServer extension method and passed a connection string as a parameter.
In the connection, you must have noticed that a database is mentioned with name “SampleDb”. This is the database that will be tested for connection health.
But we have not created it yet. So, let’s open SQL Server explorer and create a database with the same name.
Step 8 Open Server Explorer window in Visual Studio.
If your server name is correct, then a new database will be created and will be visible in the server explorer.
Step 9 Now we are done with all the configuration and database creation. Let’s run the API by pressing the F5 Key.
You will notice that weatherForecast API was called. This is because health check endpoint needs to be called explicitly.
Step 10 Run the following URL: http://localhost:50566/health
You can see that it returns Healthy status, which states that database connectivity is working fine.
Now to double check, let’s delete the database and see what happens.
I deleted the database by right clicking the database name and delete it.
Now re-run the API endpoint.
You can see that it returns Unhealthy status because we deleted the database so it is not able to connect to the connection string that we mentioned.
There are a lots of extension methods that Health Check middleware provides, and you can explore more on the google.
Thank you for reading.