Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to an API periodically to receive service health status in Asp.net Core

I have a scenario where I have to call API periodically to receive health status.

what if I use a while use?

 while (true)
  {
// API call
   var health = _client.GetHealth(1).Result;
   if (health)
     {
       some other work

     }
   Thread.Sleep(2000);
  }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is already available through the Health Checks middleware. The health checks service will check the registered probes periodically to see if everything is OK. The health status can be logged or exposed through an API endpoint.

There are several probes available. The open source AspNetCore.Diagnostics.HealthChecks library offers probes from everything from remote APIs to various database products, cloud services and more. It also offers a Health check dashboard that displays detailed health check history.

Using Health Checks is easy, as it’s already included in the web app templates.

Registering it requires adding the following to Startup.cs :

public void ConfigureServices(IServiceCollection services)
{
    services.AddHealthChecks();
}

public void Configure(IApplicationBuilder app)
{
    app.UseRouting();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapHealthChecks("/health");
    });
}

MapHealthChecks exposes a basic status at /health endpoint. To check a remote API you can add the AspNetCore.HealthChecks.Uris package and use one of the AddUrlGroup overloads:

public void ConfigureServices(IServiceCollection services)
{
    services.AddHealthChecks()
            .AddUrlGroup(new Uri("http://httpbin.org/status/200"));
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading