Que*_*n3r 6 c# .net-core asp.net-core-webapi .net-5
我为我的 .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");
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,健康端点没有出现在 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);
}
}
Run Code Online (Sandbox Code Playgroud)
在运行应用程序时 Swagger 生成了很多模型。打开运行状况端点时,页面会冻结多秒钟,因为它必须加载HealthReport对象结构。
.UseHealthChecks(HealthController.GetHealthIndication)小智 7
太长了;
在 Swagger 配置中添加 Exception 类的架构映射。
services.AddSwaggerGen(c =>
{
c.MapType<Exception>(() => new OpenApiSchema { Type = "object" });
});
Run Code Online (Sandbox Code Playgroud)
此代码会将具有 Exception 类的 Schema 转换为对象,因此您在客户端面临的行为将最小化。
解释:
根本原因: Swagger 冻结是因为 swagger javascript 客户端尝试根据从 swagger json 获取的架构创建示例模型。
如果您查看 Exception 类,它包含大量嵌套属性和类型。如果我们指示 swagger 生成器将 Exception 类视为创建的 JSON 文件中的简单对象,则客户端将不必花费时间创建嵌套对象。(或者您也可以在此转换中包含 TimeSpan 类)。
有多种方法可以解决这个问题:
我什至必须创建自己的控制器吗?还没有任何集成吗?我正在寻找解决方案。UseHealthChecks(HealthController.GetHealthInspiration)
Swashbuckle(Swagger 代码生成和 UI)使用反射和程序集文档(.xml 文件)来创建 swagger 文档,因此它不会扫描其他程序集以查找控制器。
| 归档时间: |
|
| 查看次数: |
134 次 |
| 最近记录: |