如何获取当前服务运行状况检查的URL?

时间:2020-08-04 16:54:43

标签: core asp.net-core-3.1 health-check

我需要一些有关此物品的帮助。问题如下,我需要向服务添加健康检查:

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

为此,我有一个类“ 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/officeshttp://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() 
;

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

更改此:

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

对此:

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