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

.NET 7独立工作进程中Azure TimerTrigger无法正常触发

.NET 7 TimerTrigger 函数仅触发一次后停止的问题解决

问题重现

在.NET 7中创建了一个带TimerTrigger的函数,设置为每5秒触发一次,但仅在首次运行时触发一次,之后不再执行,进程卡在Host lock lease acquired...日志行。

函数代码

[Function(nameof(FunctionSampleTimer))]
public async Task Run([TimerTrigger("*/5 * * * * *", RunOnStartup = false)] MyInfo myTimer, [DurableClient] DurableTaskClient client, FunctionContext context)
{
    var instanceid = Guid.NewGuid().ToString();
    var logger = context.GetLogger("TimerFunction");
    logger.LogInformation($"Function Ran. Next timer schedule = {myTimer.ScheduleStatus.Next}");
} 

控制台输出

[2023-02-20T01:26:52.843Z] Azure Functions .NET Worker (PID: 49120) initialized in debug mode. Waiting for debugger to attach...
[2023-02-20T01:26:53.305Z] The next 5 occurrences of the 'FunctionSampleTimer' schedule (Cron: '0,5,10,15,20,25,30,35,40,45,50,55 * * * * *') will be:
[2023-02-20T01:26:53.307Z] 02/19/2023 17:26:55-08:00 (02/20/2023 01:26:55Z)
[2023-02-20T01:26:53.308Z] 02/19/2023 17:27:00-08:00 (02/20/2023 01:27:00Z)
[2023-02-20T01:26:53.309Z] 02/19/2023 17:27:05-08:00 (02/20/2023 01:27:05Z)
[2023-02-20T01:26:53.310Z] 02/19/2023 17:27:10-08:00 (02/20/2023 01:27:10Z)
[2023-02-20T01:26:53.311Z] 02/19/2023 17:27:15-08:00 (02/20/2023 01:27:15Z)
[2023-02-20T01:26:53.312Z]
[2023-02-20T01:26:53.323Z] Host started (775ms)
[2023-02-20T01:26:53.324Z] Job host started
[2023-02-20T01:26:54.472Z] {
[2023-02-20T01:26:54.473Z]   "ProcessId": 49120,
[2023-02-20T01:26:54.474Z]   "RuntimeIdentifier": "win10-x64",
[2023-02-20T01:26:54.475Z]   "WorkerVersion": "1.8.0.0",
[2023-02-20T01:26:54.476Z]   "ProductVersion": "1.8.0-local202209270007+04ccbd8e45bb9017dc30ff5e1343e893a216e173",
[2023-02-20T01:26:54.477Z]   "FrameworkDescription": ".NET 7.0.0",
[2023-02-20T01:26:54.478Z]   "OSDescription": "Microsoft Windows 10.0.22621",
[2023-02-20T01:26:54.479Z]   "OSArchitecture": "X64",
[2023-02-20T01:26:54.480Z]   "CommandLine": "C:\Users\swkandhi\source\repos\FunctionTimerTrigger\FunctionTimerTrigger\bin\Debug\net7.0\FunctionTimerTrigger.dll --host 127.0.0.1 --port 58647 --workerId e70ddb42-819a-45b2-a17c-dca35392699d --requestId 326e359b-9d29-4105-b2f1-9ef01892ae95 --grpcMaxMessageLength 2147483647"
[2023-02-20T01:26:54.481Z] }
[2023-02-20T01:26:54.493Z] Worker process started and initialized.
[2023-02-20T01:26:55.053Z] Executing 'Functions.FunctionSampleTimer' (Reason='Timer fired at 2023-02-19T17:26:55.0194750-08:00', Id=872452e5-4d71-42ea-a16d-d4790ccf38a2)
[2023-02-20T01:26:57.821Z] Host lock lease acquired by instance ID '00000000000000000000000074E57A56'.

解决方法

  1. 修正异步函数执行上下文
    函数标记为async Task但无实际异步等待操作,会导致执行上下文未正确释放。可以添加空的异步完成语句,或者将返回类型改为void:

    [Function(nameof(FunctionSampleTimer))]
    public async Task Run([TimerTrigger("*/5 * * * * *", RunOnStartup = false)] MyInfo myTimer, [DurableClient] DurableTaskClient client, FunctionContext context)
    {
        var instanceid = Guid.NewGuid().ToString();
        var logger = context.GetLogger("TimerFunction");
        logger.LogInformation($"Function Ran. Next timer schedule = {myTimer.ScheduleStatus.Next}");
        await Task.CompletedTask; // 添加该行确保异步上下文正常结束
    } 
    
  2. 验证本地存储配置
    TimerTrigger依赖存储跟踪触发状态,确保local.settings.json中配置正确:

    {
        "IsEncrypted": false,
        "Values": {
            "AzureWebJobsStorage": "UseDevelopmentStorage=true",
            "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
        }
    }
    

    同时确认Azure Storage Emulator或Azurite处于运行状态。

  3. 升级Functions SDK版本
    当前使用的Worker版本1.8.0存在已知TimerTrigger问题,建议升级到Microsoft.Azure.Functions.Worker及相关NuGet包的最新稳定版(如1.14.x及以上)。

  4. 排除调试模式干扰
    调试模式下调试器附加可能阻塞函数执行,尝试在非调试模式下运行项目,观察是否恢复正常触发。

内容的提问来源于stack exchange,提问作者Srujan Simha Adicharla

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 11:05:19