ASP.NET Core - 尝试使用HealthChecks时出错

lmc*_*iro 6 c# asp.net-core

我正在尝试使用.NET Core 2.2运行状况检查.

ConfigureServices我注册实现Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck接口的类.

但是当我在UseHealthChecks方法中执行扩展Configure方法时,它会抛出一个错误:

public void Configure(IApplicationBuilder app)
{
    app.UseHealthChecks("/hc"); // <-- Error in this line
    // ...
Run Code Online (Sandbox Code Playgroud)

System.InvalidOperationException: '尝试激活'Microsoft.AspNetCore.Diagnostics.HealthChecks.HealthCheckMiddleware'时,无法解析类型'Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheckService'的服务

Hen*_*ema 13

您必须通过AddHealthChecks()扩展方法配置运行状况检查基础结构服务.例如:

public void ConfigureServices(IServiceCollection services)
{
    services.AddHealthChecks();
}
Run Code Online (Sandbox Code Playgroud)

另请参阅文档.

  • 请参阅 hpsanampudi 的回答。需要存储提供商。如果您没有调用 AddHealthChecks,您会收到不同的错误 (2认同)

hps*_*udi 13

在我的情况下,健康检查 UI 本身不会启动和崩溃 .net core 3.1 Web API 应用程序。

错误消息: 无法构建某些服务(验证服务描述符“ServiceType: HealthChecks.UI.Core.Notifications.IHealthCheckFailureNotifier Lifetime: Scoped ImplementationType: HealthChecks.UI.Core.Notifications.WebHookFailureNotifier”时出错:无法解析服务在尝试激活“HealthChecks.UI.Core.Notifications.WebHookFailureNotifier”时键入“HealthChecks.UI.Core.Data.HealthChecksDb”。)

修复:添加任何UI 存储提供程序。就我而言,我选择了AddInMemoryStorage()

启动文件

    public void ConfigureServices(IServiceCollection services)
    {
        ...
        
        services.AddHealthChecks() 
            .AddDbContextCheck<PollDbContext>() //nuget: Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore
            .AddApplicationInsightsPublisher(); //nuget: AspNetCore.HealthChecks.Publisher.ApplicationInsights
    
        services.AddHealthChecksUI() //nuget: AspNetCore.HealthChecks.UI
            .AddInMemoryStorage(); //nuget: AspNetCore.HealthChecks.UI.InMemory.Storage
            
        ...
    }
    
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        ...
        
        app.UseHealthChecks("/healthcheck", new HealthCheckOptions
        {
            Predicate = _ => true,
            ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse //nuget: AspNetCore.HealthChecks.UI.Client
        });
        
        //nuget: AspNetCore.HealthChecks.UI
        app.UseHealthChecksUI(options =>
        {
            options.UIPath = "/healthchecks-ui";
            options.ApiPath = "/health-ui-api";
        });
        ...
    }
Run Code Online (Sandbox Code Playgroud)

appsettings.json

    "HealthChecks-UI": {
        "DisableMigrations": true,
        "HealthChecks": [
            {
                "Name": "PollManager",
                "Uri": "/healthcheck"
            }
        ],
        "Webhooks": [
            {
                "Name": "",
                "Uri": "",
                "Payload": "",
                "RestoredPayload": ""
            }
        ],
        "EvaluationTimeOnSeconds": 10,
        "MinimumSecondsBetweenFailureNotifications": 60,
        "MaximumExecutionHistoriesPerEndpoint": 15
    }
Run Code Online (Sandbox Code Playgroud)