升级到 .NET 8 后,Azure Functions 计时器触发器配置无法解析

问题描述 投票:0回答:1

从 .NET 6 升级到 .NET 8 后,我的 Azure Functions 应用程序遇到问题。我的计时器触发器函数无法解析配置值,并且在本地运行 Function App 时出现以下错误:

Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.MyTimerFunction'.
Microsoft.Azure.WebJobs.Host: '%AppConfigOptions:MyTimerValue%' does not resolve to a value.

[2025-01-15T18:00:05.218Z] Function 'Functions.MyTimerFunction' failed indexing and will be disabled.
[2025-01-15T18:00:05.220Z] Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.AnotherTimerFunction'.
Microsoft.Azure.WebJobs.Host: '%AppConfigOptions:AnotherTimerValue%' does not resolve to a value.
...
  1. 我已经使用隔离工作模型中的 Function 属性定义了计时器触发器函数。

  2. 我的计时器的配置值存储在 host.json 文件中,如下所示:

     {
          "version": "2.0",
          "AppConfigOptions": {
            "MyTimerValue": "0 */5 * * * *",
            "AnotherTimerValue": "0 0 * * * *"
          }
        }
  1. 我的计时器功能如下所示:
[Function("MyTimerFunction")]
public void RunMyTimerFunction(
    [TimerTrigger("%AppConfigOptions:MyTimerValue%")] TimerInfo timer,
    FunctionContext context)
{
    var logger = context.GetLogger("MyTimerFunction");
    logger.LogInformation("Function executed");
}
  1. 升级到 .NET 8 后,占位符 %AppConfigOptions:MyTimerValue% 无法解析,尽管它在 .NET 6 中运行良好。

问题: 升级到 .NET 8 后,什么可能导致 %AppConfigOptions:MyTimerValue% 等配置占位符停止解析?我该如何解决这个问题,以便我的计时器触发器在隔离工作模型中按预期工作?

azure configuration azure-functions .net-8.0
1个回答
0
投票

您需要按照给定的方式在本地设置文件中添加占位符。

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
        "AppConfigOptions__MyTimerValue": "0 */2 * * * *",
        "AppConfigOptions__AnotherTimerValue": "0 0 * * * *"
  }
}

然后,在函数代码中使用它,如下所示。

[Function("MyTimerFunction")]
public void Run([TimerTrigger("%AppConfigOptions:MyTimerValue%")] TimerInfo myTimer)
{
    _logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
    
    if (myTimer.ScheduleStatus is not null)
    {
        _logger.LogInformation($"Next timer schedule at: {myTimer.ScheduleStatus.Next}");
    }
}

这将按预期工作。

Azure Functions Core Tools
Core Tools Version:       4.0.6821 Commit hash: N/A +c09a2033faa7ecf51b3773308283af0ca9a99f83 (64-bit)
Function Runtime Version: 4.1036.1.23224

[2025-01-16T14:20:50.124Z] Found C:\Users\*****\FunctionApp12\FunctionApp12\FunctionApp12.csproj. Using for user secrets file configuration.
[2025-01-16T14:20:53.081Z] Azure Functions .NET Worker (PID: 24976) initialized in debug mode. Waiting for debugger to attach...
[2025-01-16T14:20:53.118Z] Worker process started and initialized.

Functions:

        MyTimerFunction: timerTrigger

For detailed output, run func with --verbose flag.
[2025-01-16T14:20:58.140Z] Host lock lease acquired by instance ID '0000000000000000000000000D2022A4'.
[2025-01-16T14:22:00.080Z] Executing 'Functions.MyTimerFunction' (Reason='Timer fired at 2025-01-16T19:52:00.0311396+05:30', Id=ce26f2f7-6a90-47dc-b521-533a72e86837)
[2025-01-16T14:22:00.347Z] C# Timer trigger function executed at: 16-01-2025 19:52:00
[2025-01-16T14:22:00.351Z] Next timer schedule at: 16-01-2025 19:52:00
[2025-01-16T14:22:00.370Z] Executed 'Functions.MyTimerFunction' (Succeeded, Id=ce26f2f7-6a90-47dc-b521-533a72e86837, Duration=336ms)
© www.soinside.com 2019 - 2024. All rights reserved.