res*_*ute 8 .net azure-functions .net-5 health-check azure-http-trigger
我找不到任何资源来将运行状况检查添加到在 .NET 5.0 隔离中运行的 HTTPTrigger 功能应用程序。
static async Task Main()
{
var host = new HostBuilder()
.ConfigureAppConfiguration(configurationBuilder =>
{
configurationBuilder.AddEnvironmentVariables();
})
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices((builder, services) =>
{
var configuration = builder.Configuration;
services.AddDbContext(configuration);
// Add healthcheck here
services.AddHealthChecks()
// ...
// Map health checks
})
.Build();
await host.RunAsync();
}
Run Code Online (Sandbox Code Playgroud)
本指南指出我可以添加 MapHealthChecks (但在 asp.net core 中)
var app = builder.Build();
app.MapHealthChecks("/healthz");
app.Run();
Run Code Online (Sandbox Code Playgroud)
如何将其转换为在我的 dotnet 隔离应用程序中运行?
app.MapHealthChecks("/healthz");
Run Code Online (Sandbox Code Playgroud)
上面的代码创建了 ASP CORE中间件并注入IHealthCheckService以调用CheckHealthAsync()并返回 HTTP 响应。在 Azure 函数中,您可以:
注入IHealthCheckService构造函数,调用CheckHealthAsync()并返回响应。
private readonly IHealthCheckService _healthCheck;
public DependencyInjectionFunction(IHealthCheckService healthCheck)
{
_healthCheck = healthCheck;
}
[Function(nameof(DependencyInjectionFunction))]
public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestData req,
FunctionContext context)
{
var healthStatus = await _healthCheck.CheckHealthAsync();
#format health status and return HttpResponseData
}
Run Code Online (Sandbox Code Playgroud)
实现您自己的azure函数中间件,检查路径'/healthz,然后IHealthCheckService立即解析并返回健康状态