gpt4 book ai didi

.net-core - 错误 UseHealthChecksUI 遇到意外字符

转载 作者:行者123 更新时间:2023-12-05 07:22:24 25 4
gpt4 key购买 nike

我正在尝试实现 ASP.NET Core 2.2 运行状况检查功能。设置健康检查本身不是问题,但我还希望能够在其他项目中使用 UI 功能来监控我所有的 api。现在我收到异常消息

解析值时遇到意外字符:<.

我做错了什么?

API 项目:

var healthCheckOptions = new HealthCheckOptions
{
Predicate = _ => true,
ResponseWriter = async (c, r) =>
{
c.Response.ContentType = MediaTypeNames.Application.Json;
var result = JsonConvert.SerializeObject(
new
{
Checks = r.Entries.Select(e =>
new
{
Description = e.Key,
Status = e.Value.Status.ToString(),
ResponseTime = e.Value.Duration.TotalMilliseconds
}),
TotalResponseTime = r.TotalDuration.TotalMilliseconds
});
await c.Response.WriteAsync(result);
}
};

app.UseHealthChecks("/live", new HealthCheckOptions
{
Predicate = _ => true
});
app.UseHealthChecks("/hc", healthCheckOptions);
app.UseHealthChecksUI(options => options.UIPath = "/healtcheck");

// Registers required services for health checks
services
.AddHealthChecks()
.AddCheck("self", () => HealthCheckResult.Healthy())
.AddCheck("ComunContext Database", new SqlServerHealthCheck(configuration["ConnectionStrings:ComunContext"]));

网络项目:

services.AddHealthChecksUI();

app.UseHealthChecksUI(config =>
{
config.UIPath = "/healthcheck";
});

appsettings.json
{
"HealthChecks-UI": {
"HealthChecks": [
{
"Name": "Local",
"Uri": "http://localhost:27365/hc"
}
],
"EvaluationTimeOnSeconds": 10,
"MinimumSecondsBetweenFailureNotifications": 60
}
}

最佳答案

尝试添加一个 ResponseWriter:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseHealthChecks("/healthchecks", new HealthCheckOptions
{
ResponseWriter = async (context, report) =>
{
context.Response.ContentType = "application/json; charset=utf-8";
var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(report));
await context.Response.Body.WriteAsync(bytes);
}
});
app.UseHealthChecksUI();
}

关于.net-core - 错误 UseHealthChecksUI 遇到意外字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56521454/

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