使用 Azure Functions V3 (.NET 5) 的计时器触发器
Timer Trigger with Azure Functions V3 (.NET 5)
我正在使用 .NET 5 并开发 Azure Functions。通过 Visual Studio 添加新的定时器触发器功能时,它会添加一个包含以下内容的文件:
public static class Function1
{
[FunctionName("Function1")]
public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
}
}
我将它作为照片放在这里以强调它无法编译,因为 TimerTrigger class 不存在。
Intellisense 建议我添加以下内容:
using Microsoft.Azure.WebJobs;
但这没有意义,因为那是以前版本的Azure Functions。果然,Intellisense 检测到了这一点并显示了这个错误。
那么如何在 .NET 5 中使用 TimerTrigger?
可能是因为它相对较新,似乎还没有关于它的文档或热门帖子。
尝试 Microsoft.Azure.Functions.Worker.Extensions.Timer
包,如文档所述 here:
Because functions that run in a .NET isolated process use different binding types, they require a unique set of binding extension packages.
You'll find these extension packages under Microsoft.Azure.Functions.Worker.Extensions.
我在从 .Net Core 迁移到 .NET 5 时也遇到了 运行 计时器触发器的问题。以下注意事项:
- 迁移项目文件以使用 .net 5 运行时
- 添加 Nuget 包
- 修改本地主机json使用正确的worker函数
- 避免使用 CancellationToken,因为它不再提供。
(1) 项目文件:
- [TargetFramework]net5.0[/TargetFramework]
- [AzureFunctionsVersion]v3[/AzureFunctionsVersion]
- [OutputType]Exe[/OutputType]
(2) 需要 Nuget 引用:
- "Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.0.1"
- "Microsoft.Azure.Functions.Worker.Sdk" Version="1.3.0"
- "Microsoft.Azure.Functions.Worker" Version="1.6.0"
(3)
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
测试定时器触发功能:
public static class Function2
{
[Function("Function2")]
public static void Run([TimerTrigger("0 */5 * * * *")] MyInfo myTimer, FunctionContext context)
{
var logger = context.GetLogger("Function2");
logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
logger.LogInformation($"Next timer schedule at: {myTimer.ScheduleStatus.Next}");
}
}
public class MyInfo
{
public MyScheduleStatus ScheduleStatus { get; set; }
public bool IsPastDue { get; set; }
}
public class MyScheduleStatus
{
public DateTime Last { get; set; }
public DateTime Next { get; set; }
public DateTime LastUpdated { get; set; }
}
我正在使用 .NET 5 并开发 Azure Functions。通过 Visual Studio 添加新的定时器触发器功能时,它会添加一个包含以下内容的文件:
public static class Function1
{
[FunctionName("Function1")]
public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
}
}
我将它作为照片放在这里以强调它无法编译,因为 TimerTrigger class 不存在。
Intellisense 建议我添加以下内容:
using Microsoft.Azure.WebJobs;
但这没有意义,因为那是以前版本的Azure Functions。果然,Intellisense 检测到了这一点并显示了这个错误。
那么如何在 .NET 5 中使用 TimerTrigger?
可能是因为它相对较新,似乎还没有关于它的文档或热门帖子。
尝试 Microsoft.Azure.Functions.Worker.Extensions.Timer
包,如文档所述 here:
Because functions that run in a .NET isolated process use different binding types, they require a unique set of binding extension packages.
You'll find these extension packages under Microsoft.Azure.Functions.Worker.Extensions.
我在从 .Net Core 迁移到 .NET 5 时也遇到了 运行 计时器触发器的问题。以下注意事项:
- 迁移项目文件以使用 .net 5 运行时
- 添加 Nuget 包
- 修改本地主机json使用正确的worker函数
- 避免使用 CancellationToken,因为它不再提供。
(1) 项目文件:
- [TargetFramework]net5.0[/TargetFramework]
- [AzureFunctionsVersion]v3[/AzureFunctionsVersion]
- [OutputType]Exe[/OutputType]
(2) 需要 Nuget 引用:
- "Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.0.1"
- "Microsoft.Azure.Functions.Worker.Sdk" Version="1.3.0"
- "Microsoft.Azure.Functions.Worker" Version="1.6.0"
(3)
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
测试定时器触发功能:
public static class Function2
{
[Function("Function2")]
public static void Run([TimerTrigger("0 */5 * * * *")] MyInfo myTimer, FunctionContext context)
{
var logger = context.GetLogger("Function2");
logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
logger.LogInformation($"Next timer schedule at: {myTimer.ScheduleStatus.Next}");
}
}
public class MyInfo
{
public MyScheduleStatus ScheduleStatus { get; set; }
public bool IsPastDue { get; set; }
}
public class MyScheduleStatus
{
public DateTime Last { get; set; }
public DateTime Next { get; set; }
public DateTime LastUpdated { get; set; }
}