.NET Core健康检查:无法找到所需的服务

问题描述 投票:0回答:2

我正在尝试使用 Startup.cs 中的代码在示例 .NET Core 应用程序检查中注册运行状况:

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

        // Adding healthcheck in ConfigureServices
        services.AddHealthChecks(checks => 
        {
            // Custom health check which implements IHealthCheck
            // Breakpoint hits here and this code is executed
            checks.AddCheck<CustomHealthCheck>("Storage", new TimeSpan(0, 1, 0));
        });
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();
        app.UseRouting();
        app.UseAuthorization();

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

            // This throws the exception: "Unable to find the required services..."
            endpoints.MapHealthChecks("/health");
        });

        // I've tried this too. This also throws an exception
        app.UseHealthChecks("/health");
    }

但是,当我运行应用程序时,出现异常:“无法找到所需的服务。请通过在应用程序中对“ConfigureServices(...)”的调用中调用“IServiceCollection.AddHealthChecks”来添加所有所需的服务启动代码。”

我发现此异常消息令人困惑,因为我在ConfigureServices 方法中调用AddHealthChecks。所以我不确定我是否犯了错误,或者我是否需要做其他事情。

任何建议表示赞赏。

.net-core health-check
2个回答
2
投票

我刚刚重新审视了这一点。问题是第一个使用 nuget 包

Microsoft.AspNetCore.HeatlhChecks
。它应该使用 nuget 包
Microsoft.AspNetCore.Diagnostics.HealthChecks

令人困惑的是,这两个包的名称非常相似,并且都定义了接口

IHealthCheck
,因此上面的代码不会生成编译器错误消息。它只是抛出一个错误。


0
投票

仅供参考,这两个软件包现已弃用。

© www.soinside.com 2019 - 2024. All rights reserved.