You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 6自定义健康检查:动态调轮询频率及IIS启停配置方案

解决方案:动态调整.NET 6健康检查轮询间隔或通过IIS脚本定时启停修改

一、动态修改轮询间隔(无需重启服务,推荐)

基于AspNetCore.Diagnostics.HealthChecks框架,你可以通过自定义后台服务动态控制轮询频率,不需要依赖静态配置或重启服务。有两种实现思路:

思路1:修改HealthChecks.UI的配置(利用IOptionsMonitor)

通过IOptionsMonitor<HealthChecksUIOptions>实时更新轮询间隔,框架会自动感知配置变更并调整轮询频率:

  1. 注册后台服务
    在Program.cs中添加后台服务依赖:
builder.Services.AddHostedService<DynamicPollingIntervalService>();
  1. 实现后台服务逻辑
public class DynamicPollingIntervalService : BackgroundService
{
    private readonly IOptionsMonitor<HealthChecksUIOptions> _uiOptions;
    private readonly ILogger<DynamicPollingIntervalService> _logger;
    private int _currentInterval;

    public DynamicPollingIntervalService(IOptionsMonitor<HealthChecksUIOptions> uiOptions, ILogger<DynamicPollingIntervalService> logger)
    {
        _uiOptions = uiOptions;
        _logger = logger;
        _currentInterval = _uiOptions.CurrentValue.EvaluationTimeInSeconds;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            var now = DateTime.Now;
            var targetInterval = now.Hour >= 1 && now.Hour < 18 ? 10 : 60;

            if (_currentInterval != targetInterval)
            {
                // 更新配置值
                var options = _uiOptions.CurrentValue;
                options.EvaluationTimeInSeconds = targetInterval;
                _currentInterval = targetInterval;
                _logger.LogInformation("健康检查轮询间隔已更新为 {Seconds} 秒", targetInterval);
            }

            // 每分钟检查一次时间,避免频繁操作
            await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);
        }
    }
}

思路2:自定义轮询逻辑(完全可控)

禁用HealthChecks.UI的内置轮询,通过后台服务手动触发健康检查,根据时间动态调整执行间隔:

  1. 配置HealthChecks.UI时禁用内置轮询
builder.Services.AddHealthChecksUI()
    .AddInMemoryStorage()
    .SetEvaluationTimeInSeconds(0); // 关闭内置自动轮询
  1. 实现自定义调度服务
public class CustomHealthCheckScheduler : BackgroundService
{
    private readonly IHealthCheckService _healthCheckService;
    private readonly IHealthChecksUIService _uiService;
    private readonly ILogger<CustomHealthCheckScheduler> _logger;

    public CustomHealthCheckScheduler(IHealthCheckService healthCheckService, IHealthChecksUIService uiService, ILogger<CustomHealthCheckScheduler> logger)
    {
        _healthCheckService = healthCheckService;
        _uiService = uiService;
        _logger = logger;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            var now = DateTime.Now;
            var interval = now.Hour >= 1 && now.Hour < 18 ? TimeSpan.FromSeconds(10) : TimeSpan.FromMinutes(1);

            // 执行健康检查并提交结果到UI
            var healthResult = await _healthCheckService.CheckHealthAsync(stoppingToken);
            await _uiService.AddHealthCheckExecutionEntryAsync(healthResult);

            _logger.LogInformation("健康检查执行完成,下次执行间隔:{Interval}", interval);
            await Task.Delay(interval, stoppingToken);
        }
    }
}

二、通过IIS脚本定时启停修改配置(需重启服务)

如果无法修改代码,可通过PowerShell脚本+Windows任务计划实现定时启停服务、修改配置:

1. 编写PowerShell脚本

创建AdjustHealthCheckPolling.ps1,替换其中的应用池名称和配置文件路径:

# 配置参数
$appPoolName = "你的应用池名称"
$configFilePath = "C:\inetpub\wwwroot\你的应用目录\appsettings.json"
$currentHour = (Get-Date).Hour

# 确定目标轮询间隔
$targetInterval = if ($currentHour -ge 1 -and $currentHour -lt 18) { 10 } else { 60 }

# 停止IIS应用池
Stop-WebAppPool -Name $appPoolName
Write-Host "已停止应用池:$appPoolName"

# 修改配置文件
$configContent = Get-Content $configFilePath | ConvertFrom-Json
$configContent.HealthChecksUI.EvaluationTimeInSeconds = $targetInterval
$configContent | ConvertTo-Json -Depth 10 | Set-Content $configFilePath
Write-Host "已将轮询间隔更新为:$targetInterval 秒"

# 启动IIS应用池
Start-WebAppPool -Name $appPoolName
Write-Host "已启动应用池:$appPoolName"

2. 创建Windows任务计划

  • 打开「任务计划程序」,点击「创建基本任务」
  • 触发器设置为每天1:00和每天18:00各执行一次
  • 操作选择「启动程序」,程序/脚本填写powershell.exe,添加参数:
    -ExecutionPolicy Bypass -File "C:\脚本存放路径\AdjustHealthCheckPolling.ps1"
    
  • 配置任务运行账户为具有IIS管理权限的账户(如本地管理员)

方案对比

方案优点缺点
动态修改轮询间隔无需重启,用户无感知,实时生效需要修改代码实现
IIS脚本启停修改无需修改代码服务会短暂中断,影响用户

内容的提问来源于stack exchange,提问作者CodeTruthSeeker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 19:01:20