7

We have to write multiple triggers. I was hoping to create separate functions, based on the trigger types. So if I need 5 timer triggers, that will run at different times, I would create one Timer trigger function class and name the functions like [TimerTrigger1], [TimerTrigger2], [TimerTrigger3] ... and so forth. After I added the code I am not sure if I can do that anymore.

Can someone suggest how I can go about adding multiple triggers? I can't have two Run functions under one class.

public static class TimerTrigger
{
    [FunctionName("InsertTimerTrigger1")]
    public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
    {
         // Do task 1
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
    }

    [FunctionName("InsertTimerTrigger2")]
    public static void Run([TimerTrigger("0 */15 * * * *")]TimerInfo myTimer, ILogger log)
    {
        //Do Task 2
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
    }
}

2 Answers 2

14

You can create multiple functions in Single Class. You can change Run Method name.

public static class Function1
{
    [FunctionName("Function1")]
    public static void Method1([TimerTrigger("0 */2 * * * *")]TimerInfo myTimer, TraceWriter log)
    {
        log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
    }

    [FunctionName("Function2")]
    public static void Method2([TimerTrigger("0 */3 * * * *")]TimerInfo myTimer, TraceWriter log)
    {
        log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
    }
}

But I will recommend, Create multiple functions will help you (5 in your case).

  1. If you are using common business logic, you can put in a common class and inject in all function.
  2. You can independently Enable/Disable/Delete function from FunctionApp Instance.

enter image description here

  1. You can monitor each function independently (from Function Monitor section)

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @Pankaj, I am a bit confused about FunctionApp instance and Function Monitor Section? Can you please give an example of those. I am developing all in visual studio 2019 .net core project.
Also, how can I enable/Disable functions?
@Sarah I added Screenshot from Azure portal. Once you will deploy your function App you can see Monitor and Enable/Disable Tab from the portal.
that was really helpful. I got my functions running based on different timers within one class now.
@Sarah Please Accept my answer if this was helpful for you.
7

You can choose any name for the methods. (Naming it as "Run" is not a requirement.)

public static class TimerTrigger
{
    [FunctionName("InsertTimerTrigger1")]
    public static void InsertTimerTrigger1([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
    {
         // Do task 1
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
    }

    [FunctionName("InsertTimerTrigger2")]
    public static void InsertTimerTrigger2([TimerTrigger("0 */15 * * * *")]TimerInfo myTimer, ILogger log)
    {
        //Do Task 2
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.