Manikanta Reddy D Manikanta Reddy D - 5 months ago 50
C# Question

Configurable Timer Triggers - Azure Web Jobs

I'm building a job to trigger at some regular interval (say 1 minute). I've successfully used triggered web jobs with time-span hard coded in the functions.

public void foo([TimerTrigger("00:01:00")] TimerInfo timer)

Now if I ever want to change the trigger time from 1-min to 2-min I've to redeploy the code. Instead is there a way to make the TimeTrigger Configurable, from a config file.

Note that replacing the string with a dynamically read value isn't possible as the TimerTrigger Attribute is a const string expression or a Type.

Answer Source

After much digging I've realized that this can be done with an SDK extension class TimerSchedule.

For it, you would need a base class that you'll be able to use for multiple triggers.

class CustomTimerTriggerBase: TimerSchedule
    TimeSpan timer;
    public CustomTimerTriggerBase(string triggerConfigKey)

    public override DateTime GetNextOccurrence(DateTime now)
        return now.Add(timer);

Use this Base to generate your timers...

public sealed class FooTimer : CustomTimerTriggerBase
    public FooTimer() : base("FooTimerKey") {}

In your App.config have a key for "FooTimer"

<add key="FooTimerKey" value="00:02:00" />

Use this FooTimer class in your webjob functions.

public void foo([TimerTrigger(typeof(FooTimer)] TimerInfo timer)

Now you can simply change the value in app config instead of redeploying the code. NOTE: Since you are using Timespan to parse, the string can be of any format you need as defined in TimeSpan formats.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download