c# - ASP.NET EF Core 健康检查只返回 200 状态
问题描述
我目前正在使用 Docker 容器下的 PostgreSQL 和 EntifyFramework Core 对我的 Identity ASP.NET Core 3.1 项目进行健康检查。
这是我的项目中安装的 nuget 包
- Microsoft.EntityFrameworkCore 3.1.3
- Npgsql.EntityFrameworkCore.PostgreSQL 3.1.3
- Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore 3.1.3
- Microsoft.AspNetCore.Identity.EntityFrameworkCore 3.1.3
这是我的 Startup.cs 类
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<IdentityContext>(options => options.UseNpgsql(Configuration["Identity:ConnectionString"]));
services.AddHealthChecks()
.AddDbContextCheck<IdentityContext>("Database");
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseHealthChecks("/health");
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
一切正常,我通过访问 /health 端点获得了一个带有 200 状态代码的健康响应,直到我故意停止 Docker 中的 PostgreSQL 容器。
我期望从 /health 收到带有不健康响应的 503 状态代码,但收到带有 200 状态代码的空白响应
解决方案
我认为当您请求 url “/health” 时未调用您的“数据库”检查。尝试使用标签注册 HealthCheck。然后用这个标签定义端点。
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<IdentityContext>(options => options.UseNpgsql(Configuration["Identity:ConnectionString"]));
services.AddHealthChecks()
.AddDbContextCheck<IdentityContext>("Database",tags: new[] { "live" });
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHealthChecks("/health/live", new HealthCheckOptions()
{
Predicate = (check) => check.Tags.Contains("live")
});
});
}
您可以在此处阅读有关健康检查的信息
推荐阅读
- bash - 我无法使用“超时命令”获得“退出状态”
- javascript - 'return' 没有输出所有结果
- excel - Excel 到 PPT 与执行直到
- java - OpenJDK 上的密钥库的 Vault 初始化问题
- angular - 当产品自动添加到标题组件中的购物车时更新购物车计数而不刷新角度 6 中的页面
- python - Python/ Pandas/ DataFrame/ Iteration/ 遍历行,找到一个值,记下索引,然后从那个索引开始,找到另一个值
- mapbox - 如何发送 WMS 功能请求
- android - 将 SDCard URI 与 SAF 或 StorageVolume API 返回的 USBStorage URI 区分开来
- rest - 使用 OPENTEXT REST API 上传文档不起作用
- php - 如何将数组转换为json解码格式