add all the required services by calling 'IServiceCollection.AddHealthChecks'

12

I am using ASP.NET Core 3, .NET Core 3.0.100, Visual Studio 2019 Community. I follow this guide https://docs.microsoft.com/en-us/aspnet/core/release-notes/aspnetcore-3.0?view=aspnetcore-3.0#health-checks

In Startup.cs, I add 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");
            });
        }
    }
}

Error

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 foo.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 foo.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()

How to fix it?

c#
asp.net-core
blazor
.net-core-3.0
asp.net-core-3.0
asked on Stack Overflow Oct 3, 2019 by Do Nhu Vy • edited Oct 23, 2019 by Do Nhu Vy

1 Answer

16

The exception message clearly indicates that your should invoke services.AddHealthChecks() inside ConfigureServices(IServiceCollection services)

Please add all the required services by calling 'IServiceCollection.AddHealthChecks' inside the call to 'ConfigureServices(...)' in the application startup code.

void ConfigureServices(IServiceCollection services) {

    //...

    services.AddHealthChecks();

    //...
}

Reference Health checks in ASP.NET Core

answered on Stack Overflow Oct 4, 2019 by Nkosi

User contributions licensed under CC BY-SA 3.0