.Net Core 2.2运行状况检查UI给出空白页

时间:2019-12-10 13:41:09

标签: asp.net-core

我会跟踪我能找到的所有文章,包括这篇Microsoft Health Checks article
运行状况检查正常运行,并且应用程序的/ health url返回json,运行状况正常。
但是,/ healthchecks-ui返回空白页。在控制台开发人员工具上,我可以在.healthchecks-bundle.js:1中看到错误“ Uncaught SyntaxError:Unexpected token {”。

Startup.cs

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

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<Model>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        // Health checks services
        services
            .AddHealthChecks()
            .AddMemoryHealthCheck("memory")
            .AddSqlServer(Configuration["ConnectionStrings:DefaultConnection"]);

        services.AddHealthChecksUI();    
        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            //app.UseBrowserLink();
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

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

         app.UseMvc(routes =>
         {
             routes.MapRoute(
                 name: "default",
                 template: "{controller=Home}/{action=Index}/{id?}");

             routes.MapRoute(
                 name: "defaultApi",
                 template: "api/{controller=MyController}/{action=Get}/{value?}");
         });
    }
}

appsettings.json

{
    "HealthChecksUI": {
         "HealthChecks": [
         {
            "Name": "HealthChecksService",
            "Uri": "http://localhost:42008/health"
         }
     ],
     "Webhooks": [],
     "EvaluationTimeInSeconds": 10,
     "MinimumSecondsBetweenFailureNotifications": 60
  }
}

我也尝试过使用HealthChecks-UI,但这没有影响。
我当然包括了nuget包Microsoft.AspNetCore.Diagnostics.HealthChecks(2.2.0),AspNetCore.HealthChecks.UI(2.2.35)。
同样,/ health返回json表示应用程序运行正常,但UI返回带有js错误的空白页。 (在chrome和IE上都尝试过)。
该环境是封闭的,因此如果ui尝试从Internet引用外部资源,它将失败,但是我看不到这样的调用。

2 个答案:

答案 0 :(得分:1)

这对我有用: 我正在将Netcore 2.2与AspNetCore.HealthChecks.UI Version = 2.2.2。一起使用。

在我的Configure方法中:

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

app.UseHealthChecksUI(setup =>
{
    setup.UIPath = "/healthchecks-ui"; // ui path
    setup.ApiPath = "/health-ui-api"; // this is the internal ui api
});

appsettings.json

"HealthChecks-UI": {
      "HealthChecks": [
        {
          "Name": "HealthChecksService",
            "Uri": "http://localhost:42008/healthz"
        }
      ],
      "Webhooks": [],
      "EvaluationTimeOnSeconds": 120,
      "MinimumSecondsBetweenFailureNotifications": 3660
  }

答案 1 :(得分:1)

您所做的工作有两个问题:

  1. 您需要使用HealthChecks.UI.Configuration.Options方法正确设置UseHealthChecksUI。您需要覆盖默认路径以及UseRelativeApiPathUseRelativeResourcesPath设置。

  2. 您要在浏览器中查看UI的路径(根据您的设置)应为 / health-ui

这是我用来使示例工作的代码:

appsettings.json

{
    "HealthChecksUI": {
         "HealthChecks": [
         {
            "Name": "HealthChecksService",
            "Uri": "http://localhost:42008/health"
         }
     ],
     "Webhooks": [],
     "EvaluationTimeInSeconds": 10,
     "MinimumSecondsBetweenFailureNotifications": 60
  }
}

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    ...
    services
        .AddHealthChecks()
        .AddMemoryHealthCheck("memory")
        .AddSqlServer(Configuration["ConnectionStrings:DefaultConnection"]);

    services.AddHealthChecksUI();
    ...
}

public void Configure(IApplicationBuilder app)
{
    ... 
    app.UseCustomHealthChecks();
    ...
}

ApplicationBuilderExtensions.cs

public static IApplicationBuilder UseCustomHealthChecks(this IApplicationBuilder builder)
{
    const string healthCheckApiPath = "/health";
    return builder
        .UseHealthChecks(healthCheckApiPath,
            new HealthCheckOptions()
            {
                Predicate = _ => true,
                ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
            })
        .UseHealthChecksUI(options =>
        {
            options.UIPath = $"{healthCheckApiPath}-ui";
            options.ApiPath = $"{healthCheckApiPath}-api";
            options.UseRelativeApiPath = false;
            options.UseRelativeResourcesPath = false;
        });
}

HTH