0

I need some help with this item. The problem is as follows, I need to add a healthcheck to my service:

// Add the health checks.
services.AddHealthChecks()
        .AddSqlServer(Configuration["ConnectionStrings:MyConnectionString"])
        .AddCheck("Offices Health Check", new OfficeHealthCheck(), HealthStatus.Unhealthy)
;

For this I have the class "OfficeHealthCheck.cs" which implements IHealthCheck and defines the following function:

private async Task<bool> GetOffices()
{
    bool isHealthy = false;

    Uri apiUri = new Uri("http://localhost:58355/api/offices");

    using (HttpClient client = new HttpClient())
    {
        var result = await client.GetAsync(apiUri);

        if (result.IsSuccessStatusCode)
            isHealthy = true;
    }

    return isHealthy;
}

The problem I'm trying to solve is how to change the "localhost:58355" to the current server where I am running the service (both the healthcheck and the enpoint i'm calling are part of the same service), for example http://myproductionserver.com/api/offices or http://mystageserver.org/api/offices and so on...

I read some articles that mentioned adding a singleton with , but I have failed to implement IHttpContextAccessor correctly. I have added the singleton and added the part in the object as follows:

public class OfficeHealthCheck : IHealthCheck
    {
private readonly IHttpContextAccessor _httpContextAccesor;

public RegionHealthCheck(IHttpContextAccessor httpContextAccessor) { 
    _httpContextAccesor = httpContextAccessor
}

But now its asking me to pass an instance of IHttpContextAccessor to the constructor in this line which I don't know how to do:

// Add the health checks.
        .AddCheck("Offices Health Check", new OfficeHealthCheck() 
;

Any help would be appreciated

jps
  • 20,041
  • 15
  • 75
  • 79
Jalex23
  • 159
  • 1
  • 7

1 Answers1

0

Change this:

// Add the health checks.
        .AddCheck("Offices Health Check", new OfficeHealthCheck() 

To this:

// Add the health checks.
        .AddCheck<OfficeHealthCheck>("Offices Health Check") 
Jalex23
  • 159
  • 1
  • 7