如何对 ASP.NET/blazor 中的 POST url 进行健康检查
我正在尝试在我的 blazor 应用程序中实施健康检查。为此,我使用了 Microsoft.AspNetCore.Diagnostics.HealthChecks 包等。下面您可以看到 sql 和 url 健康检查。
startup.cs
//using AjuaBlazorServerApp.Data;
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace AjuaBlazorServerApp
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddHostedService<PeriodicExecutor>();
services.AddHealthChecks().AddUrlGroup(new Uri("https://api.example.com/post"),
name: "Example Endpoint",
failureStatus: HealthStatus.Degraded)
.AddSqlServer(Configuration["sqlString"],
healthQuery: "select 1",
failureStatus: HealthStatus.Degraded,
name: "SQL Server");
services.AddHealthChecksUI(opt =>
{
opt.SetEvaluationTimeInSeconds(5); //time in seconds between check
opt.MaximumHistoryEntriesPerEndpoint(60); //maximum history of checks
opt.SetApiMaxActiveRequests(1); //api requests concurrency
opt.AddHealthCheckEndpoint("Ajua API", "/api/health"); //map health check api
}).AddInMemoryStorage();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
endpoints.MapHealthChecks("/api/health", new HealthCheckOptions()
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.
WriteHealthCheckUIResponse
});
endpoints.MapHealthChecksUI();
});
}
}
}
sql 运行得很好。但是,url 运行状况检查返回以下错误:
Discover endpoint #0 is not responding with code in 200...299 range, the current status is MethodNotAllowed.
我想知道是否有一种方法可以设置方法类型,以及是否需要将一些测试详细信息发送到端点,以便我们实际上可以获得有效的响应。
I am trying to implement health checks in my blazor application. To do so, I have used the Microsoft.AspNetCore.Diagnostics.HealthChecks
package among others. Below you can see sql and url health checks.
startup.cs
//using AjuaBlazorServerApp.Data;
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace AjuaBlazorServerApp
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddHostedService<PeriodicExecutor>();
services.AddHealthChecks().AddUrlGroup(new Uri("https://api.example.com/post"),
name: "Example Endpoint",
failureStatus: HealthStatus.Degraded)
.AddSqlServer(Configuration["sqlString"],
healthQuery: "select 1",
failureStatus: HealthStatus.Degraded,
name: "SQL Server");
services.AddHealthChecksUI(opt =>
{
opt.SetEvaluationTimeInSeconds(5); //time in seconds between check
opt.MaximumHistoryEntriesPerEndpoint(60); //maximum history of checks
opt.SetApiMaxActiveRequests(1); //api requests concurrency
opt.AddHealthCheckEndpoint("Ajua API", "/api/health"); //map health check api
}).AddInMemoryStorage();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
endpoints.MapHealthChecks("/api/health", new HealthCheckOptions()
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.
WriteHealthCheckUIResponse
});
endpoints.MapHealthChecksUI();
});
}
}
}
The sql one works perfectly. However the url health check returns the following error:
Discover endpoint #0 is not responding with code in 200...299 range, the current status is MethodNotAllowed.
What i would like to know is if there is a way to maybe set the method type and if need be send some test details to the endpoint so that we can actually get a valid response.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AddUrlGroup 具有重载 允许您通过
httpMethod
参数指定方法。尝试使用:另一个重载 允许显式配置 HttpClient 和 HttpMessageHandler,以添加特定的默认标头,例如,启用压缩或重定向。
又一个重载< /a> 允许显式配置 UriHealthCheckOptions 类生成其他
AddUrlGroup
重载:无法指定内容标头,因为运行状况检查代码 不发送正文。
AddUrlGroup has an overload that allows you to specify the method through the
httpMethod
parameter. Try using :Another overload allows configuring the HttpClient and HttpMessageHandler explicitly, to add specific default headers for example, enable compression or redirection.
Yet another overload allows explicitly configuring the UriHealthCheckOptions class generated by other
AddUrlGroup
overloads:There's no way to specify content headers because the health check code doesn't send a body.
我接受上面的答案,因为它从技术上回答了我的问题。然而,这是我最终使用的实现。基本上,您必须创建自己的自定义健康检查。
2. 在该文件夹中创建一个新类,并添加类似于下面的代码
EndpointHealth.cs,
然后将以下代码添加到startup.cs 文件的顶部<代码>使用BlazorServerApp.HealthChecks;
最后是下面的代码:
I accepted the answer above because it technically answered my question. However, this is the implementation I ended up using. Basically you will have to create your own custom healthcheck.
2. Create a new class in that folder and add code similar to what I have below
EndpointHealth.cs
Then add the following code to the top of your startup.cs file
using BlazorServerApp.HealthChecks;
and finally the below code: