为 AspNetCore 加载 HealthCheck UI 时出错

鲁伊兹德

我在使用 AspNetCore.HealthChecks.UI 包时遇到了一些问题。我在我的API项目的Startup类中进行了必要的设置,但是当我访问端点查看HealthCheck界面时,只显示了一个JSON。

我的健康检查用户界面

我的项目是 .NET Core 3.1 版,我在其中安装了以下 nuget 包:

  • AspNetCore.HealthChecks.UI v3.1.3
  • AspNetCore.HealthChecks.UI.Client v3.1.2
  • AspNetCore.HealthChecks.UI.InMemory.Storage v3.1.2
  • Microsoft.Extensions.Diagnostics.HealthChecks v5.0.9

下面是我使用 HealthCheck 设置的扩展方法:

public static IServiceCollection AddHealthCheckConfiguration(
            this IServiceCollection services, 
            IConfiguration configuration)
        {
            string mongoConnectionString = configuration.GetSection("MongoSettings").GetSection("Connection").Value;
            string mongoDatabaseName = configuration.GetSection("MongoSettings").GetSection("DatabaseName").Value;
            string redisConnectionString = configuration.GetConnectionString("Redis");

            services
                .AddHealthChecks()
                .AddRedis(redisConnectionString)
                .AddMongoDb(mongoConnectionString, mongoDatabaseName: mongoDatabaseName);

            services.AddHealthChecksUI(opt =>
            {
                opt.SetEvaluationTimeInSeconds(15);
                opt.MaximumHistoryEntriesPerEndpoint(60);
                opt.SetApiMaxActiveRequests(1);

                opt.AddHealthCheckEndpoint("default api", "/api-health");
            })
            .AddInMemoryStorage();

            return services;
        }

在另一种扩展方法中,我添加了用于显示界面的必要设置:

public static IApplicationBuilder UseMvcConfiguration(this IApplicationBuilder app)
        {
            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

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

                endpoints.MapHealthChecks("/api-health", new HealthCheckOptions
                {
                    Predicate = _ => true,
                    ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
                });

                endpoints.MapHealthChecksUI();
            });

            return app;
        }

最后,我的 Startup 类如下所示:

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddApiResponseCompressionConfig();

            services.AddApiCachingConfig(Configuration);

            services.AddHateoasConfig();

            services.AddMongoDbConfig();

            services.AddAutoMapper(typeof(Startup));

            services.AddWebApiConfig();

            services.AddHealthCheckConfiguration(Configuration);

            services.ResolveGeneralDependencies();

            services.ResolveRepositories();

            services.ResolveApplicationServices();

            services.AddSwaggerConfig();
        }

        public void Configure(
            IApplicationBuilder app, 
            IWebHostEnvironment env, 
            IApiVersionDescriptionProvider provider)
        {
            app.UseResponseCompression();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMiddleware<ExceptionMiddleware>();

            app.UseHealthChecks("/healthcheck");

            app.UseMvcConfiguration();

            app.UseSwaggerConfig(provider);
        }
    }

我在 StackOverflow 上搜索了类似的错误,但我找到的答案都没有解决我的问题。

鲁伊兹德

感谢@fbede 的帮助,我设法解决了这个问题。

我没有正确映射健康检查生成端点和监控接口本身。

有必要对我在问题中提出的代码进行调整,从我的扩展方法开始:

public static IServiceCollection AddHealthCheckConfiguration(
            this IServiceCollection services, 
            IConfiguration configuration)
        {
            string mongoConnectionString = configuration.GetSection("MongoSettings").GetSection("Connection").Value;
            string mongoDatabaseName = configuration.GetSection("MongoSettings").GetSection("DatabaseName").Value;

            services
                .AddHealthChecks()
                .AddMongoDb(mongoConnectionString, mongoDatabaseName: mongoDatabaseName);

            services.AddHealthChecksUI().AddInMemoryStorage();

            return services;
        }

并以我的 Startup 类的 Configure 方法结束:

public void Configure(
            IApplicationBuilder app, 
            IWebHostEnvironment env, 
            IApiVersionDescriptionProvider provider)
        {
            app.UseResponseCompression();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHealthChecks("/healthcheck", new HealthCheckOptions()
            {
                Predicate = _ => true,
                ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
            });

            app.UseHealthChecksUI(options =>
            {
                options.UIPath = "/healthchecks-ui";
                options.ApiPath = "/health-ui-api";
            });

            app.UseMiddleware<ExceptionMiddleware>();

            app.UseMvcConfiguration();

            app.UseSwaggerConfig(provider);
        }
    }

另外,我删除了以下不必要的配置:

app.UseEndpoints(endpoints =>
            {    
                endpoints.MapHealthChecks("/api-health", new HealthCheckOptions
                {
                    Predicate = _ => true,
                    ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
                });

                endpoints.MapHealthChecksUI();
            });

这些修复导致界面的预期加载:

健康检查界面

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章