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



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

// 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";到我运行该服务的当前服务器(healthcheck和我调用的enpoint都是同一服务的一部分(,例如http://myproductionserver.com/api/offices或http://mystageserver.org/api/offices等等…

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

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") 

相关内容

最新更新