[英]Timer Trigger with Azure Functions V3 (.NET 5)
I am using .NET 5 and developing Azure Functions.我正在使用 .NET 5 并开发 Azure 函数。 When adding a new Timer Trigger function through Visual Studio it adds a file with this content:
通过 Visual Studio 添加新的定时器触发器 function 时,它会添加一个包含以下内容的文件:
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}");
}
}
I'm including it as a photo here to highlight that it doesn't compile because the TimerTrigger class doesn't exist.我在这里将它作为照片包括在内,以强调它无法编译,因为 TimerTrigger class 不存在。
Intellisense suggests that I add the following using: Intellisense 建议我添加以下使用:
using Microsoft.Azure.WebJobs;
But that doesn't make sense, because that belongs to the previous version of Azure Functions.但是那没有意义,因为那是属于上一版的 Azure 函数。 And sure enough, Intellisense detects this and show this error.
果然,Intellisense 检测到了这一点并显示了这个错误。
So how do I use TimerTrigger in .NET 5?那么如何在.NET 5中使用TimerTrigger呢?
Maybe because this is relatively new, there seems to be no documentation or popular posts about this yet.也许因为这是相对较新的,似乎还没有关于它的文档或热门帖子。
Try the Microsoft.Azure.Functions.Worker.Extensions.Timer
package, as documented here :尝试
Microsoft.Azure.Functions.Worker.Extensions.Timer
包,如此处所述:
Because functions that run in a .NET isolated process use different binding types, they require a unique set of binding extension packages.
由于在 .NET 隔离进程中运行的函数使用不同的绑定类型,因此它们需要一组唯一的绑定扩展包。
You'll find these extension packages under Microsoft.Azure.Functions.Worker.Extensions.
您将在 Microsoft.Azure.Functions.Worker.Extensions 下找到这些扩展包。
I had also trouble with running timer trigger when migrating from.Net Core to .NET 5. Following attention points:从.Net Core 迁移到 .NET 时,我也遇到了运行定时器触发器的问题 5. 以下注意事项:
(1) Project File: (1) 项目文件:
- [TargetFramework]net5.0[/TargetFramework]
- [AzureFunctionsVersion]v3[/AzureFunctionsVersion]
- [OutputType]Exe[/OutputType]
(2) Nuget References Required: (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) (3)
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
Test timer trigger function:测试计时器触发器 function:
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; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.