简体   繁体   English

具有 Azure 函数的定时器触发器 V3 (.NET 5)

[英]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 下找到这些扩展包。

Direct link to NuGet 直接链接到 NuGet

I had also trouble with running timer trigger when migrating from.Net Core to .NET 5. Following attention points:从.Net Core 迁移到 .NET 时,我也遇到了运行定时器触发器的问题 5. 以下注意事项:

  1. Migration of the project file to use .net 5 runtime项目文件迁移使用 .net 5 runtime
  2. Adding Nuget Packages添加 Nuget 包
  3. Amendment of the local host json to use the correct function worker修改本地主机 json 以使用正确的 function worker
  4. Avoid using CancellationToken since it isn't provided any longer.避免使用 CancellationToken,因为它不再提供。

(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.

相关问题 Azure functions v3 /HTTP trigger functions: Limiting the request body and URL 大小 - Azure functions v3 /HTTP trigger functions : Limiting the request body and URL size Azure 函数运行时 v3 中间件 - Azure Functions Runtime v3 Middleware Azure Functions V3 进程外 - App Insights DI - Azure Functions V3 Out-of-Process - App Insights DI 我可以使用 Azure Functions V3 Azure static web API - Can I use Azure Functions V3 Azure static web API 如何在隔离的 Azure 函数 v4 中获取消息元数据 - c# 中的队列触发器 - How to get message metadata in isolated Azure functions v4 - Queue Trigger in c# 节点 Azure Function Timer Trigger Always Timesout - Node Azure Function Timer Trigger Always Timesout Azure Function 带定时器触发器和 sql 输入绑定 - Azure Function with Timer Trigger and sql input binding 当 email 到达时(V3)不会在不同的资源组中触发 - When email arrives (V3) wont trigger in different resource group Azure 认知服务 sdk 在 chrome 扩展清单 v3 中的实施 - Azure Cognitive Services sdk implementation in chrome extension manifest v3 Azure 媒体服务 Fairplay V3 DRM 内容密钥政策 - Azure Media Service Fairplay V3 DRM Content Key Policy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM