简体   繁体   English

使用 HealthCheckUI 失败并出现异常“没有注册类型‘HealthChecks.UI.Core.Data.HealthChecksDb’的服务。”

[英]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.

相关问题 如何创建自定义类的单例并将其注入.Net core 2.0中的其他类? “没有注册“服务”类型的服务。”? - How to create a singleton of a custom class and inject it other classes in .Net core 2.0? 'No service for type 'Services' has been registered.'? “ InvalidOperationException:没有注册类型&#39;Microsoft.Extensions.Configuration.IConfiguration&#39;的服务。” - “InvalidOperationException: No service for type 'Microsoft.Extensions.Configuration.IConfiguration' has been registered.” System.InvalidOperationException:“没有为类型 'Microsoft.AspNetCore.Hosting.Server.IServer' 注册服务。” - System.InvalidOperationException: 'No service for type 'Microsoft.AspNetCore.Hosting.Server.IServer' has been registered.' c#: 没有注册 *Context 类型的服务。 数据库连接问题 - c#: No service for type *Context has been registered. Database connection issue 当 RegisterEventBus “没有注册 Autofac.ILifetimeScope 类型的服务时。” - When RegisterEventBus " No service for type Autofac.ILifetimeScope' has been registered.' ASP.NET 内核中的错误:没有注册类型 […] 的服务 - Error in ASP.NET Core : no service for type […] has been registered 没有注册类型服务 - No service for type has been registered 没有注册类型“类型”的服务 - No service for type 'Type' has been registered 没有服务类型<type>已注册</type> - No service for type <TYPE> has been registered 自有服务 - 没有注册类型服务 - Own service - no service for type has been registered
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM