【发布时间】:2021-05-28 21:57:42
【问题描述】:
我为我的 .Net 5 Web API 项目启用了健康检查
public sealed class Startup
{
public void ConfigureServices(IServiceCollection serviceCollection)
{
serviceCollection.AddHealthChecks();
// ...
}
public void Configure(IApplicationBuilder applicationBuilder, IWebHostEnvironment webHostEnvironment)
{
// ...
applicationBuilder.UseHealthChecks("/health");
// ...
}
}
不幸的是,健康端点没有出现在 swagger 文档中。这就是为什么我创建了一个额外的控制器
[ApiController]
[Route("[controller]")]
public sealed class HealthController : ControllerBase
{
private readonly HealthCheckService _healthCheckService;
public HealthController(HealthCheckService healthCheckService)
{
_healthCheckService = healthCheckService;
}
[HttpGet]
public async Task<ActionResult<HealthReport>> GetHealthIndication()
{
HealthReport healthReport = await _healthCheckService.CheckHealthAsync();
if (healthReport.Status == HealthStatus.Healthy)
{
return Ok(healthReport);
}
int serviceUnavailableStatusCode = (int) HttpStatusCode.ServiceUnavailable;
return StatusCode(serviceUnavailableStatusCode, healthReport);
}
}
在运行应用程序时,Swagger 生成了很多模型。打开健康端点时,页面会冻结几秒钟,因为它必须加载 HealthReport 对象结构。
- 我认为具有单个端点的 API 会因此而冻结是不好的……对此有什么建议吗?
- 我什至必须创建自己的控制器,还没有任何集成吗?我正在寻找像
.UseHealthChecks(HealthController.GetHealthIndication)这样的解决方案
【问题讨论】:
标签: c# .net-core asp.net-core-webapi .net-5