简体   繁体   中英

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. 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:

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.
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 c(IWebHost host, CancellationToken token) at Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(IWebHost host)
at fzag.portal.api.Program.Main(String[] args) in C:\Repos***\Program.cs:line 26

This exception only occurs when using AddHealthChecksUI() . I think the API is trying to persist data, thus it searches for an instance of HealthChecksDb (which is a DbContext). But why does AddHealthChecksUI() not register the DbContext itself? And what am I doing wrong?

You need to specify where the data of the HealthCheckUI should be persisted. Here is an example with a SQL Server:

services.AddHealthChecksUI().AddSqlServerStorage(Configuration.GetConnectionString("HealthChecks"));

The newly added package being:

<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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM