从 .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.
...
我已经使用隔离工作模型中的 Function 属性定义了计时器触发器函数。
我的计时器的配置值存储在 host.json 文件中,如下所示:
{
"version": "2.0",
"AppConfigOptions": {
"MyTimerValue": "0 */5 * * * *",
"AnotherTimerValue": "0 0 * * * *"
}
}
[Function("MyTimerFunction")]
public void RunMyTimerFunction(
[TimerTrigger("%AppConfigOptions:MyTimerValue%")] TimerInfo timer,
FunctionContext context)
{
var logger = context.GetLogger("MyTimerFunction");
logger.LogInformation("Function executed");
}
问题: 升级到 .NET 8 后,什么可能导致 %AppConfigOptions:MyTimerValue% 等配置占位符停止解析?我该如何解决这个问题,以便我的计时器触发器在隔离工作模型中按预期工作?
您需要按照给定的方式在本地设置文件中添加占位符。
{
"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)