c# - ASP.NET Core - 尝试使用 HealthChecks 时出错
问题描述
我正在尝试使用 .NET Core 2.2 健康检查。
在ConfigureServices
我注册了实现Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck
接口的类。
但是当我在UseHealthChecks
方法内部执行扩展方法时Configure
,它会抛出一个错误:
public void Configure(IApplicationBuilder app)
{
app.UseHealthChecks("/hc"); // <-- Error in this line
// ...
System.InvalidOperationException: “尝试激活“Microsoft.AspNetCore.Diagnostics.HealthChecks.HealthCheckMiddleware”时无法解析“Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheckService”类型的服务
解决方案
在我的情况下,运行状况检查 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()
启动.cs
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";
});
...
}
应用设置.json
"HealthChecks-UI": {
"DisableMigrations": true,
"HealthChecks": [
{
"Name": "PollManager",
"Uri": "/healthcheck"
}
],
"Webhooks": [
{
"Name": "",
"Uri": "",
"Payload": "",
"RestoredPayload": ""
}
],
"EvaluationTimeOnSeconds": 10,
"MinimumSecondsBetweenFailureNotifications": 60,
"MaximumExecutionHistoriesPerEndpoint": 15
}
推荐阅读
- flutter - 移动应用程序中的 GPS 精度问题
- azure - Azure 存储身份验证失败 - 观众验证失败。观众不匹配。[天蓝色][OAuth]
- android - 安卓工作室 | 4.2 | 北极狐 | 不显示可调试的进程
- android - Android Studio Gradle:请从您的构建脚本中删除 `jcenter()` Maven 存储库的使用/JCenter 已结束生命周期
- java - java.lang.UnsupportedOperationException:删除
- python-3.x - 在 django 我得到 AttributeError: module 'django.contrib.admin' has no attribute 'display'
- php - 自动缩进在 PHP 模式下不工作 vscode
- sql-server - 创建返回表的函数不起作用语法错误?SQL
- c# - 如何使用 EditorFor 保存对模型的更改?
- asp.net-core - Nginx 反向代理 - 405 POST