【问题标题】:Timer Trigger with Azure Functions V3 (.NET 5)使用 Azure Functions V3 (.NET 5) 的计时器触发器
【发布时间】:2021-07-13 10:41:43
【问题描述】:

我正在使用 .NET 5 并开发 Azure Functions。通过 Visual Studio 添加新的 Timer Trigger 函数时,它会添加一个包含以下内容的文件:

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 类不存在。

Intellisense 建议我使用以下方法添加以下内容:

using Microsoft.Azure.WebJobs;

但这没有意义,因为它属于以前版本的 Azure Functions。果然,Intellisense 检测到这一点并显示此错误。

那么如何在 .NET 5 中使用 TimerTrigger?

也许因为这是相对较新的,似乎还没有关于此的文档或热门帖子。

【问题讨论】:

    标签: c# azure azure-functions .net-5


    【解决方案1】:

    尝试Microsoft.Azure.Functions.Worker.Extensions.Timer 包,如here 所述:

    因为在 .NET 隔离进程中运行的函数使用不同的绑定类型,它们需要一组独特的绑定扩展包。

    您可以在 Microsoft.Azure.Functions.Worker.Extensions 下找到这些扩展包。

    Direct link to NuGet

    【讨论】:

    • 警告:当我添加 TimerTrigger 时,我的函数无法再在本地运行。删除它后,我能够在本地再次运行它。我尝试重新启动 VS 和整个计算机无济于事,所以很确定它的 TimerTrigger。
    • 您是否从github.com/Azure/azure-functions-core-tools 安装了最新的SDK?
    • Afaik 不适用于核心和 5.0 版本,所以我建议您检查一下。
    • 其实还是失败了。当 TimerTrigger 在启动时为“PastDue”时会发生这种情况,然后它会锁定并且永远不会正确启动,然后崩溃或冻结几分钟,然后关闭。
    • 我想是时候open an issue了。实际上,它看起来像this one
    【解决方案2】:

    从 .Net Core 迁移到 .NET 5 时,我也遇到了运行计时器触发器的问题。以下注意点:

    1. 迁移项目文件以使用 .net 5 运行时
    2. 添加 Nuget 包
    3. 修改本地主机 json 以使用正确的 function worker
    4. 避免使用 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; }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-20
      • 2017-10-29
      • 2022-01-19
      • 2018-10-26
      • 2017-10-09
      • 2022-06-10
      • 2020-02-11
      • 1970-01-01
      相关资源
      最近更新 更多