如何获取当前服务健康检查的URL?

How to get URL for current service health check?

我需要一些关于这个项目的帮助。问题如下,我需要为我的服务添加健康检查:

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

为此,我有 class“OfficeHealthCheck.cs”,它实现了 IHealthCheck 并定义了以下函数:

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;
}

我要解决的问题是如何将“localhost:58355”更改为我所在的当前服务器 运行 服务(健康检查和我调用的 enpoint 都是一部分相同的服务),例如 http://myproductionserver.com/api/offices or http://mystageserver.org/api/offices 等等...

我阅读了一些文章,其中提到使用 添加单例,但我未能正确实现 IHttpContextAccessor。我添加了单例并在对象中添加了如下部分:

public class OfficeHealthCheck : IHealthCheck
    {
private readonly IHttpContextAccessor _httpContextAccesor;

public RegionHealthCheck(IHttpContextAccessor httpContextAccessor) { 
    _httpContextAccesor = httpContextAccessor
}

但现在它要求我将 IHttpContextAccessor 的实例传递给此行中的构造函数,我不知道该怎么做:

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

任何帮助将不胜感激

改变这个:

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

为此:

// Add the health checks.
        .AddCheck<OfficeHealthCheck>("Offices Health Check")