ASP.NET EF Core健康检查返回的仅是200状态。
创始人
2024-09-16 06:00:17
0

在ASP.NET EF Core中,健康检查返回的默认状态码是200。如果希望修改返回的状态码,可以通过自定义HealthCheckOptions来实现。

以下是一个示例代码,演示如何修改健康检查返回的状态码为204:

public void ConfigureServices(IServiceCollection services)
{
    // 添加EF Core服务
    services.AddDbContext(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    // 添加健康检查服务
    services.AddHealthChecks()
        .AddDbContextCheck()
        .AddCheck("custom_health_check");

    // 添加健康检查中间件
    services.AddHealthChecksUI()
        .AddSqlServerStorage(Configuration.GetConnectionString("DefaultConnection"));

    // 配置健康检查返回的状态码
    services.Configure(options =>
    {
        options.ReturnHttp200EvenIfUnhealthy = false;
    });

    services.AddControllers();
    services.AddRazorPages();
}

在上述示例中,我们通过services.Configure方法配置了ReturnHttp200EvenIfUnhealthy属性为false,这样健康检查返回的状态码就会根据实际情况返回。

如果希望返回其他状态码,可以将ReturnHttp200EvenIfUnhealthy属性设置为true,并自定义健康检查中间件的返回逻辑。以下是一个示例代码,演示如何返回状态码为500的健康检查结果:

public class CustomHealthCheckMiddleware
{
    private readonly RequestDelegate _next;
    private readonly IHealthChecksBuilder _healthChecksBuilder;

    public CustomHealthCheckMiddleware(RequestDelegate next, IHealthChecksBuilder healthChecksBuilder)
    {
        _next = next;
        _healthChecksBuilder = healthChecksBuilder;
    }

    public async Task Invoke(HttpContext context)
    {
        var healthReport = await _healthChecksBuilder.HealthChecks.Build().CheckHealthAsync();

        if (healthReport.Status != HealthStatus.Healthy)
        {
            context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
        }
        else
        {
            context.Response.StatusCode = (int)HttpStatusCode.OK;
        }
        
        await context.Response.WriteAsync(healthReport.Status.ToString());
    }
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // ...

    app.UseRouting();

    // 添加自定义健康检查中间件
    app.UseMiddleware(app.ApplicationServices.GetRequiredService());

    // ...

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
        endpoints.MapRazorPages();
    });
}

在上述示例中,我们自定义了一个名为CustomHealthCheckMiddleware的中间件,用于检查健康状态并返回相应的状态码。根据健康状态,我们设置了响应的状态码为500(不健康)或200(健康),并将健康状态返回给客户端。

请根据实际需求修改上述示例代码,以满足您的健康检查返回状态码的要求。

相关内容

热门资讯

透视阶段!wepoker私人局... 透视阶段!wepoker私人局透视(德普之星介绍)好像真的是有辅助技巧(哔哩哔哩)1、下载好wepo...
透视要领!德普之星辅助器怎么用... 透视要领!德普之星辅助器怎么用(WePoKer黑科技)确实真的是有辅助软件(哔哩哔哩)1、全新机制【...
透视窍要!hardrock透视... 您好,hardrock透视工具这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302...
透视绝活!hhpoker德州透... 透视绝活!hhpoker德州透视挂(AApoker插件)确实是有辅助插件(哔哩哔哩)1、起透看视 h...
透视指南书!hhpoker买挂... 透视指南书!hhpoker买挂(WePoKer助手)一贯是有辅助神器(哔哩哔哩)1、hhpoker买...
透视攻略!wepoker究竟有... 透视攻略!wepoker究竟有没有透视(HHpoker有用)果然存在有辅助攻略(哔哩哔哩)wepok...
透视窍要!wepoker免费脚... 透视窍要!wepoker免费脚本咨询(WePoKer工具)都是存在有辅助插件(哔哩哔哩)1、wepo...
透视阶段!hhpoker开挂教... 透视阶段!hhpoker开挂教程(WePoKer app)竟然真的有辅助神器(哔哩哔哩)hhpoke...
透视办法!wpk辅助软件(WP... 透视办法!wpk辅助软件(WPK透视版)切实是真的有辅助技巧(哔哩哔哩)1、wpk辅助软件免费脚本咨...
透视积累!德州hhpoker是... 透视积累!德州hhpoker是真的吗(WePoKer辅助器)切实是有辅助脚本(哔哩哔哩)1、德州hh...