如何在Azure WebJob中动态设置多个计时器触发时间

时间:2017-09-15 12:42:42

标签: azure-webjobs timer-trigger

我的解决方案如下。请检查以下几点。

1 个答案:

答案 0 :(得分:1)

  public class Program
  {
    public static void Main()
    {
        JobHostConfiguration config = new JobHostConfiguration();
        config.NameResolver = new TimeResolver();
        config.UseTimers();
        JobHost host = new JobHost(config);
        host.RunAndBlock();
    }

    private class TimeResolver : INameResolver
    {
        public string Resolve(string name)
        {
            string value = string.Empty;
            switch (name)
            {
                case "TimerJob":
                    Console.WriteLine("Name Is TimerJob : " + name);
                    value = "00:00:10";
                    break;
                case "TimerJobAlter":
                    Console.WriteLine("Name Is TimerJobAlter : " + name);
                    value = "00:00:20";
                    break;
            }
            return value;
        }
    }


    //Runs once every 30 seconds
    public static void TimerJob([TimerTrigger("%TimerJob%")] TimerInfo timer)
    {
        Console.WriteLine("Timer1 job fired!");
    }

    // Runs once every 60 seconds
    public static void TimerJobAlter([TimerTrigger("%TimerJobAlter%")] TimerInfo timer)
    {
        Console.WriteLine("Timer2 job fired!");
    }

}