Blog

Filter posts by Category Or Tag of the Blog section!

Health checks in ASP.NET Core

Tuesday, 16 March 2021

Recently, I had a task in a project to check the availability of the external APIs, I started writing a custom scheduler to call the targeted APIs and let the administrator know in a case of failed response. After writing the mentioned code I realized that there is a predefined service exactly for the same goal! In ASP.NET Core health checks are used to monitor the health of an application, by periodically checking its critical dependencies such as databases, external APIs, or services. Health checks can be used to detect and diagnose problems before they become critical.

As I mentioned, ASP.NET Core provides built-in support for adding health checks to your application, which you can use to monitor the health of your application and its dependencies. You can also create custom health checks to monitor any aspect of your application that you need. Here's an example of how to add a health check to your ASP.NET Core application:

 

  1. In your Startup.cs file, add the health checks services to the ConfigureServices method:

public void ConfigureServices(IServiceCollection services)

{

       services.AddHealthChecks();

}

 

  1. Next, you can add health checks for your dependencies. For example, if you want to check the health of a SQL Server database, you can use the AddSqlServer method:

public void ConfigureServices(IServiceCollection services)

{

      services.AddHealthChecks()

        .AddSqlServer(Configuration.GetConnectionString("MyDb"));

}

 

  1. Finally, you can expose the health checks endpoint by adding middleware to your Configure method:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

{

      app.UseHealthChecks("/health");

}

 

With this config, you can now access the health checks endpoint at http://localhost:5000/health. This will return a JSON object containing the status of each health check. You can also use the HealthCheckService class to programmatically check the health of your application or to retrieve the results of the health checks. Note that there are other ways to customize health checks in ASP.NET Core, such as specifying a timeout or configuring the response format. 

 

public void ConfigureServices(IServiceCollection services)

{

    services.AddHttpClient("external_api", c =>

    {

        c.BaseAddress = new Uri("https://external-api.com");

    });

    services.AddHealthChecks()

        .AddUrlGroup(new Uri("https://external-api.com/health"), name: "External API");

}



public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

{

    app.UseRouting();

    app.UseEndpoints(endpoints =>

    {

        endpoints.MapHealthChecks("/health");

    });

}

 

In the ConfigureServices method, we first register an HttpClient with the name "external_api" and set its base address to the external API's URL. Then, we add a health check to the HealthChecks service using the AddUrlGroup method, which will check the external API's health endpoint at the URL "https://external-api.com/health". We also give this health check the name of "External API".

In the Configure method, we add an endpoint for the health checks at the path "/health" using the MapHealthChecks method. Now, when we navigate to "https://our-app.com/health", we should see the results of the health checks, including the status of the "External API" check.

 

comments powered by Disqus