将HealthCheck端点集成到dotnet核心上的swagger(开放API)UI中 [英] Integrating HealthCheck endpoint into swagger (open API) UI on dotnet core

查看:155
本文介绍了将HealthCheck端点集成到dotnet核心上的swagger(开放API)UI中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

I am using Dotnet Core healthchecks as described here. In short, it looks like this:

首先,您要像这样配置服务:

First, you configure services like this:

        services.AddHealthChecks()
            .AddSqlServer("connectionString", name: "SQlServerHealthCheck")
            ... // Add multiple other checks

然后,您像这样注册一个端点:

Then, you register an endpoint like this:

        app.UseHealthChecks("/my/healthCheck/endpoint");

我们还使用了Swagger(又名Open API),我们通过Swagger UI看到了所有端点,但是没有通过运行状况检查端点.

We are also using Swagger (aka Open API) and we see all the endpoints via Swagger UI, but not the health check endpoint.

是否有一种方法可以将其添加到控制器方法中,以便Swagger自动获取端点,或者以其他方式将其与swagger集成?

Is there a way to add this to a controller method so that Swagger picks up the endpoint automatically, or maybe integrate it with swagger in another way?

到目前为止,我发现最好的解决方案是添加一个自定义的硬编码终结点(这里),但是维护起来不太方便.

The best solution I found so far is to add a custom hardcoded endpoint (like described here), but it is not nice to maintain.

推荐答案

仍在寻找更好的解决方案,但是穷人对这个问题的解决方案看起来像这样:

Still looking for a better solution, but a poor man's solution to this problem looks like this:

    public const string HealthCheckEndpoint = "/my/healthCheck/endpoint";

    public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
    {
        var pathItem = new PathItem();
        pathItem.Get = new Operation()
        {
            Tags = new[] { "ApiHealth" },
            Produces = new[] { "application/json" }
        };

        var properties = new Dictionary<string, Schema>();
        properties.Add("status", new Schema(){ Type = "string" });
        properties.Add("errors", new Schema(){ Type = "array" });

        var exampleObject = new { status = "Healthy", errors = new List<string>()};

        pathItem.Get.Responses = new Dictionary<string, Response>();
        pathItem.Get.Responses.Add("200", new Response() {
            Description = "OK",
            Schema = new Schema() {
                Properties = properties,
                Example = exampleObject }});

        swaggerDoc.Paths.Add(HealthCheckEndpoint, pathItem);
    }

这篇关于将HealthCheck端点集成到dotnet核心上的swagger(开放API)UI中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆