健康检查端点生成的HealthReport对象结构冻结Swagger文档页面

c#

我为我的 .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)
以上是健康检查端点生成的HealthReport对象结构冻结Swagger文档页面的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>