[英]Using HealthCheckUI fails with exception "No service for type 'HealthChecks.UI.Core.Data.HealthChecksDb' has been registered."
I am using a .NET Core 3.1 Web app.我正在使用 .NET Core 3.1 Web 应用程序。 The following packages are installed:安装了以下软件包:
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.HealthChecks" Version="2.2.0" />
<PackageReference Include="AspNetCore.HealthChecks.UI" Version="3.1.2" />
Inside my Startup.cs class I register my HealthChecks like this:在我的 Startup.cs class 中,我像这样注册我的 HealthChecks:
services.AddHealthChecksUI();
services.AddHealthChecks()
.AddCheck("SQL Server", new SqlServerConnectionHealthCheck(connectionString), HealthStatus.Unhealthy)
.AddCheck("Redis", new RedisConnectionHealthCheck(), HealthStatus.Unhealthy)
.AddCheck("CRM", new CrmConnectionHealthCheck(), HealthStatus.Unhealthy)
.AddCheck("DMS", new DmsConnectionHealthCheck(), HealthStatus.Unhealthy);
app.UseEndpoints(endpointRouteBuilder =>
{
endpointRouteBuilder.MapControllers();
endpointRouteBuilder.MapHealthChecks("/health");
endpointRouteBuilder.MapHealthChecksUI(options =>
{
options.UIPath = "/healthui";
});
}
Sadly as soon as my application starts, I am getting the following exception:遗憾的是,一旦我的应用程序启动,我就会收到以下异常:
Unhandled exception.未处理的异常。 System.InvalidOperationException: No service for type 'HealthChecks.UI.Core.Data.HealthChecksDb' has been registered. System.InvalidOperationException:没有注册“HealthChecks.UI.Core.Data.HealthChecksDb”类型的服务。
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at HealthChecks.UI.Core.HostedService.UIInitializationHostedService.InitializeDatabase(IServiceProvider sp) at HealthChecks.UI.Core.HostedService.UIInitializationHostedService.StartAsync(CancellationToken cancellationToken) at Microsoft.AspNetCore.Hosting.HostedServiceExecutor.ExecuteAsync(Func`2 callback, Boolean throwOnFirstFailure) at Microsoft.AspNetCore.Hosting.WebHost.StartAsync(CancellationToken cancellationToken) at Microsoft.AspNetCore.Hosting.WebHostExtensions.RunAsync(IWebHost host, CancellationToken token, String startupMessage) at Microsoft.AspNetCore.Hosting.WebHostExtensions.RunAsync(IWebHost host, CancellationToken token, String startupMessage) at Microsoft.AspNetCore.Hosting.WebHostExtensions.RunAsyn在 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider 提供者,类型 serviceType) 在 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider 提供者)在 HealthChecks.UI.Core.HostedService.UIInitializationHostedService.StartAsync(CancellationToken cancellationToken)在 Microsoft.AspNetCore.Hosting.HostedServiceExecutor.ExecuteAsync(Func`2 回调,Boolean throwOnFirstFailure)在 Microsoft.AspNetCore.Hosting.WebHost.StartAsync(CancellationToken)在 cancellationToken Microsoft.AspNetCore.Hosting.WebHostExtensions.RunAsync(IWebHost 主机、CancellationToken 令牌、字符串 startupMessage)在 Microsoft.AspNetCore.Hosting.WebHostExtensions.RunAsync(IWebHost 主机、CancellationToken 令牌、字符串 startupMessage) c(IWebHost host, CancellationToken token) at Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(IWebHost host) c(IWebHost 主机,CancellationToken 令牌)在 Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(IWebHost 主机)
at fzag.portal.api.Program.Main(String[] args) in C:\Repos***\Program.cs:line 26在 fzag.portal.api.Program.Main(String[] args) 在 C:\Repos***\Program.cs:line 26
This exception only occurs when using AddHealthChecksUI()
.此异常仅在使用AddHealthChecksUI()
时发生。 I think the API is trying to persist data, thus it searches for an instance of HealthChecksDb
(which is a DbContext).我认为 API 试图保留数据,因此它搜索HealthChecksDb
的实例(这是一个 DbContext)。 But why does AddHealthChecksUI()
not register the DbContext itself?但是为什么AddHealthChecksUI()
不注册 DbContext 本身呢? And what am I doing wrong?我做错了什么?
You need to specify where the data of the HealthCheckUI should be persisted.您需要指定应将 HealthCheckUI 的数据保存在何处。 Here is an example with a SQL Server:这是 SQL 服务器的示例:
services.AddHealthChecksUI().AddSqlServerStorage(Configuration.GetConnectionString("HealthChecks"));
The newly added package being:新增的package为:
<PackageReference Include="AspNetCore.HealthChecks.UI.SqlServer.Storage" Version="3.1.2" />
There are different providers available, each one coming from another Nuget package. You can find more information here: https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks#ui-storage-providers有不同的供应商,每一个都来自另一个 Nuget package。您可以在这里找到更多信息: https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks#ui-storage-providers
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.