Search code examples
azureazure-webjobstimer-trigger

Want to trigger Different methods at different time interval in Azure Web Job


I have hosted my web job through website (with option Added Existing project as a Azure Webjob). I am want to trigger different methods at different time interval.

Code :

static void Main()
{
    var config = new JobHostConfiguration();
    config.UseTimers();   
    var host = new JobHost();
    host.RunAndBlock();
}

    public static void TimerTrig1([TimerTrigger("00:00:02")] TimerInfo timer)
    {
        Console.WriteLine("Triggered");
    }  

    public static void TimerTrig2([TimerTrigger("00:00:04")] TimerInfo timer)
    {
        Console.WriteLine("Triggered");
    }  

and webjob-publish-settings.json is :

        {
         "$schema": "http://schemastore.org/schemas/json/webjob-publish-settings.json",
         "webJobName": "KentDataImporterWebJob",
         "runMode": "Continuous"
        }

I need a solution so that I can trigger these function on 2 Sec or 4 Sec.

So I want to trigger different methods on different interval in Azure Web Job when my azure web job hosted with different web Site.


Solution

  • Your code looks fine. A couple of caveats:

    Make sure that in the Main method of your Program class, you call the config.UseTimers() method. If you do not, your TimerTriggers will not fire. Also, make sure that your WebJob is continuous.

    public static void Main()
    {
        var config = new JobHostConfiguration();
        config.UseTimers();
        var host = new JobHost(config);
        host.RunAndBlock();
    }