Niels Brinch
Niels Brinch

Reputation: 3622

Timer Trigger with Azure Functions V3 (.NET 5)

I am using .NET 5 and developing Azure Functions. When adding a new Timer Trigger function through Visual Studio it adds a file with this content:

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.

Intellisense suggests that I add the following using:

using Microsoft.Azure.WebJobs;

But that doesn't make sense, because that belongs to the previous version of Azure Functions. And sure enough, Intellisense detects this and show this error.

enter image description here

So how do I use TimerTrigger in .NET 5?

Maybe because this is relatively new, there seems to be no documentation or popular posts about this yet.

Upvotes: 9

Views: 5034

Answers (2)

Mheriff
Mheriff

Reputation: 111

I had also trouble with running timer trigger when migrating from .Net Core to .NET 5. Following attention points:

  1. Migration of the project file to use .net 5 runtime
  2. Adding Nuget Packages
  3. Amendment of the local host json to use the correct function worker
  4. Avoid using CancellationToken since it isn't provided any longer.

(1) Project File:

- [TargetFramework]net5.0[/TargetFramework]
- [AzureFunctionsVersion]v3[/AzureFunctionsVersion]
- [OutputType]Exe[/OutputType]

(2) Nuget References Required:

- "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"

Test timer trigger 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; }
    }

Upvotes: 1

Peter Bons
Peter Bons

Reputation: 29780

Try the Microsoft.Azure.Functions.Worker.Extensions.Timer package, as documented 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.

Direct link to NuGet

Upvotes: 9

Related Questions