gpt4 book ai didi

c# - 通过调用 'IServiceCollection.AddHealthChecks' 添加所有需要的服务

转载 作者:行者123 更新时间:2023-11-30 21:27:10 25 4
gpt4 key购买 nike

我正在使用 ASP.NET Core 3、.NET Core 3.0.100、Visual Studio 2019 Community。我遵循本指南 https://learn.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 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()

如何解决?

最佳答案

异常消息清楚地表明您应该在 ConfigureServices(IServiceCollection services) 中调用 services.AddHealthChecks()

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();

//...
}

引用Health checks in ASP.NET Core

关于c# - 通过调用 'IServiceCollection.AddHealthChecks' 添加所有需要的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58228245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com