为 AspNetCore 加载 HealthCheck UI 时出错

Error loading HealthCheck UI for AspNetCore

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

My HealthCheck UI

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

下面是我使用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 class 看起来像这样:

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);
        }
    }

我在 Whosebug 上搜索了类似的错误,但是 none 我找到的答案解决了我的问题。

感谢@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 方法结束 class:

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();
            });

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