我如何每天在特定时间使用Azure WebJob中的计时器调用方法?

问题描述 投票:2回答:1

我需要在Run()方法内调用Main()吗?因此它将在代码中提到的时间每天调用。

public class Program
{
    private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);


    static void Main()
    {
        var host = new JobHost();
        // The following code ensures that the WebJob will be running continuously

        host.RunAndBlock();
    }

    // This method will be called on weekly basis
    public static void Run([TimerTrigger(typeof(MyDailySchedule))] TimerInfo timerInfo, TextWriter log)
    {

        log4net.Config.XmlConfigurator.Configure();
        try
        {
            MainA.Wait();
        }
        catch (Exception ex)
        {

        }

    }
    static async Task MainA()
    {

        WebJob1 Service = new WebJob1();
        await Service.DeletData();

    }
}  

public class MyDailySchedule : DailySchedule
{
    public MyDailySchedule() :
        //Schedule
base("2:00:00", "14:00:00", "15:00:00")

    { }
}
c# azure-webjobs azure-webjobssdk
1个回答
2
投票

您无需使用WebJobs SDK即可实现。相反:

  • 编写一个简单的控制台应用程序,该应用程序在启动时直接执行您需要做的事情(即,不使用任何JobHost)。
  • 使用cron表达式将其部署为预定的WebJob(有关详细信息,请参见doc
© www.soinside.com 2019 - 2024. All rights reserved.