代码之家  ›  专栏  ›  技术社区  ›  Vy Do

通过调用'iservicecollection.addhealthchecks'添加所有必需的服务

  •  1
  • Vy Do  · 技术社区  · 5 年前

    我正在使用ASP.NET Core 3,.NET Core 3.0.100,Visual Studio 2019社区。我遵循这个指南 https://docs.microsoft.com/en-us/aspnet/core/release-notes/aspnetcore-3.0?view=aspnetcore-3.0#health-checks

    Startup.cs ,我补充说 endpoints.MapHealthChecks("/health");

            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {         
    
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                    app.UseDatabaseErrorPage();
                }
                else
                {
                    app.UseExceptionHandler("/Error");
                    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                    app.UseHsts();
                }
                app.UseHttpsRedirection();
                app.UseStaticFiles();
                app.UseRouting();
                app.UseAuthentication();
                app.UseAuthorization();
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                    endpoints.MapRazorPages();
                    endpoints.MapBlazorHub();
                    endpoints.MapFallbackToPage("/_Host");
                    endpoints.MapHealthChecks("/health");
                });
            }
        }
    }
    

    错误

    System.InvalidOperationException
      HResult=0x80131509
      Message=Unable to find the required services. Please add all the required services by calling 'IServiceCollection.AddHealthChecks' inside the call to 'ConfigureServices(...)' in the application startup code.
      Source=Microsoft.AspNetCore.Diagnostics.HealthChecks
      StackTrace:
       at Microsoft.AspNetCore.Builder.HealthCheckEndpointRouteBuilderExtensions.MapHealthChecksCore(IEndpointRouteBuilder endpoints, String pattern, HealthCheckOptions options)
       at Microsoft.AspNetCore.Builder.HealthCheckEndpointRouteBuilderExtensions.MapHealthChecks(IEndpointRouteBuilder endpoints, String pattern)
       at acc133blazor.Startup.<>c.<Configure>b__5_0(IEndpointRouteBuilder endpoints) in C:\Users\donhuvy\Desktop\acc133b3\acc133blazor\Startup.cs:line 104
       at Microsoft.AspNetCore.Builder.EndpointRoutingApplicationBuilderExtensions.UseEndpoints(IApplicationBuilder builder, Action`1 configure)
       at acc133blazor.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env) in C:\Users\donhuvy\Desktop\acc133b3\acc133blazor\Startup.cs:line 98
       at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
       at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.<Build>b__0(IApplicationBuilder builder)
       at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass13_0.<UseStartup>b__2(IApplicationBuilder app)
       at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)
       at Microsoft.AspNetCore.Server.IIS.Core.IISServerSetupFilter.<>c__DisplayClass2_0.<Configure>b__0(IApplicationBuilder app)
       at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
       at Microsoft.AspNetCore.Hosting.GenericWebHostService.<StartAsync>d__31.MoveNext()
    

    enter image description here

    怎么解决?

    1 回复  |  直到 5 年前
        1
  •  1
  •   Nkosi    5 年前

    异常消息清楚地指示您应该调用 services.AddHealthChecks() 里面 ConfigureServices(IServiceCollection services)

    请通过调用应用程序启动代码中的“configureservices(…)”内的“iservicecollection.addhealthchecks”来添加所有必需的服务。

    void ConfigureServices(IServiceCollection services) {
    
        //...
    
        services.AddHealthChecks();
    
        //...
    }
    

    参考 Health checks in ASP.NET Core

    推荐文章