0

我找不到任何资源来将运行状况检查添加到在 .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();
}

指南指出我可以添加 MapHealthChecks(但在 asp.net 核心中)

var app = builder.Build();

app.MapHealthChecks("/healthz");

app.Run();

如何将其翻译为在我的 dotnet 隔离应用程序中运行?

4

1 回答 1

1
app.MapHealthChecks("/healthz");

上面的代码创建了 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
      }
    
  • 实现自己的 azure 函数中间件,检查路径 '/healthz 然后解析IHealthCheckService并立即返回健康状态

于 2022-02-01T21:08:15.467 回答