Azure Function App健康检查配置故障:两种方案均未生效
问题描述
我未找到任何包含正确答案的资料(已尝试两种方案,但似乎都无效),因此在此发布我的问题。
我正在使用时间触发的Azure Function检查其他端点的健康状态(端点信息将从配置中读取),为此我使用了包——Microsoft.Extensions.Diagnostics.HealthChecks。
方案1
Startup.cs 类
using Microsoft.Azure.Functions.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection; [assembly: FunctionsStartup(typeof(HealthChecker.Startup))] namespace HealthChecker { public class Startup : FunctionsStartup { public override void Configure(IFunctionsHostBuilder builder) { // 获取IConfiguration实例 var configuration = builder.GetContext().Configuration; // 注册健康检查 builder.Services.AddHealthChecks(); // 调用定义健康检查的方法 HealthCheckDefinitions.ConfigureHealthChecks(builder.Services.AddHealthChecks(), configuration); } } }
HealthCheckDefinitions.cs 类
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; using System; namespace HealthChecker { public static class HealthCheckDefinitions { public static void ConfigureHealthChecks(IHealthChecksBuilder builder, IConfiguration configuration) { try { builder.AddCheck("test", () => { bool isHealthy = false; // 执行测试以检查与端点的连接性 // ..... if (isHealthy) return HealthCheckResult.Healthy("API connection is healthy."); else return HealthCheckResult.Unhealthy("API connection is unhealthy."); }); } catch(Exception ex) { throw ex; } } } }
此处的问题是:尽管ConfigureHealthChecks方法被调用,但程序从未进入builder.AddCheck()逻辑。
方案2
我遵循官方文档尝试如下实现:
Startup.cs 类
using Microsoft.Azure.Functions.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection; [assembly: FunctionsStartup(typeof(HealthChecker.Startup))] namespace HealthChecker { public class Startup : FunctionsStartup { public override void Configure(IFunctionsHostBuilder builder) { // 获取IConfiguration实例 var configuration = builder.GetContext().Configuration; // 注册健康检查 builder.Services.AddHealthChecks().AddCheck<SampleHealthCheck>("SampleHealthCheckName"); ; } } }
SampleHealthCheck.cs 类
using Microsoft.Extensions.Diagnostics.HealthChecks; using System.Threading; using System.Threading.Tasks; namespace HealthChecker { public class SampleHealthCheck : IHealthCheck { public Task<HealthCheckResult> CheckHealthAsync( HealthCheckContext context, CancellationToken cancellationToken = default) { var isHealthy = true; // ... if (isHealthy) { return Task.FromResult( HealthCheckResult.Healthy("A healthy result.")); } return Task.FromResult( new HealthCheckResult( context.Registration.FailureStatus, "An unhealthy result.")); } } }
此处同样存在问题:程序从未进入SampleHealthCheck.cs类的CheckHealthAsync()方法。
我哪里操作错误了?
问题根源及解决方法
核心问题是只完成了健康检查的注册,但没有在时间触发的Function中主动执行这些健康检查。
Microsoft.Extensions.Diagnostics.HealthChecks的注册只是把检查逻辑注入到DI容器中,并不会自动执行——你需要在Function的触发方法里,通过HealthCheckService来调用这些检查逻辑。
具体修正步骤
- 在时间触发Function类中,注入
HealthCheckService并执行检查:
using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Azure.WebJobs; using Microsoft.Extensions.Logging; namespace HealthChecker { public class HealthCheckFunction { private readonly HealthCheckService _healthCheckService; // 构造函数注入HealthCheckService public HealthCheckFunction(HealthCheckService healthCheckService) { _healthCheckService = healthCheckService; } [FunctionName("HealthCheckTrigger")] public async Task Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log) { log.LogInformation($"健康检查开始执行: {DateTime.Now}"); // 执行所有注册的健康检查 var healthReport = await _healthCheckService.CheckHealthAsync(); // 处理并输出检查结果 foreach (var entry in healthReport.Entries) { log.LogInformation($"检查项 {entry.Key}: {entry.Value.Status} - {entry.Value.Description}"); } } } }
- 修正方案1的注册错误:
方案1中两次调用builder.Services.AddHealthChecks(),会创建新的IHealthChecksBuilder实例,导致添加的检查未绑定到正确容器。修改后的Startup.cs:
public override void Configure(IFunctionsHostBuilder builder) { var configuration = builder.GetContext().Configuration; // 仅调用一次AddHealthChecks,获取正确的Builder实例 var healthCheckBuilder = builder.Services.AddHealthChecks(); HealthCheckDefinitions.ConfigureHealthChecks(healthCheckBuilder, configuration); }
- 确保依赖包版本匹配:
除Microsoft.Extensions.Diagnostics.HealthChecks外,需保证Microsoft.Azure.Functions.Extensions.DependencyInjection包版本与Azure Functions Runtime版本兼容。
为什么之前的代码不工作?
- 方案1中重复调用
AddHealthChecks(),导致检查逻辑绑定到了未被容器使用的Builder实例,相当于无效注册。 - 方案2注册逻辑正确,但没有在Function触发时主动调用
HealthCheckService执行检查,因此CheckHealthAsync方法永远不会被触发。
内容的提问来源于stack exchange,提问作者The Inquisitive Coder
相关产品推荐
相关产品推荐

