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

How can I call a method daily at particular time using a timer in an Azure WebJob?

我需要在 Main() 方法中调用 Run() 吗?所以它会在代码中提到的时间每天调用。

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")

    { }
}

您不需要使用 WebJobs SDK 来实现此目的。相反:

  • 编写一个简单的控制台应用程序,在启动时直接执行您需要的操作(即不使用任何 JobHost)。
  • 使用 cron 表达式将其部署为计划的 WebJob(有关详细信息,请参阅 doc)。