通过调用“IServiceCollection.AddHealthChecks”添加所有必需的服务
Posted
技术标签:
【中文标题】通过调用“IServiceCollection.AddHealthChecks”添加所有必需的服务【英文标题】:add all the required services by calling 'IServiceCollection.AddHealthChecks' 【发布时间】:2020-02-02 07:27:37 【问题描述】:我正在使用 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 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()
如何解决?
【问题讨论】:
当我不小心从 Startup.cs 中删除了一行时,我收到了这个;) 【参考方案1】:异常消息清楚地表明您应该在ConfigureServices(IServiceCollection services)
内调用services.AddHealthChecks()
请通过在应用程序启动代码中对“ConfigureServices(...)”的调用中调用“IServiceCollection.AddHealthChecks”来添加所有必需的服务。
void ConfigureServices(IServiceCollection services)
//...
services.AddHealthChecks();
//...
参考Health checks in ASP.NET Core
【讨论】:
好答案。只看到了一半的解决方案【参考方案2】:您很可能忘记添加到 ConfigureServices:
services.AddMvc();
【讨论】:
以上是关于通过调用“IServiceCollection.AddHealthChecks”添加所有必需的服务的主要内容,如果未能解决你的问题,请参考以下文章
如何通过 DBLINK 通过 DBLINK 调用 SELECT?
通过self调用propery和直接调用propery的区别