我正在开发应该持续运行的 azure webjob。我有一个公共(public)静态功能。我希望此功能在没有任何队列的情况下自动触发。现在我正在使用while(true)来连续运行。还有其他方法吗?

请在下面找到我的代码

   static void Main()
    {
        var host = new JobHost();
        host.Call(typeof(Functions).GetMethod("ProcessMethod"));
        // The following code ensures that the WebJob will be running continuously
        host.RunAndBlock();
    }

[NoAutomaticTriggerAttribute]
public static void ProcessMethod(TextWriter log)
{
    while (true)
    {
        try
        {
            log.WriteLine("There are {0} pending requests", pendings.Count);
        }
        catch (Exception ex)
        {
            log.WriteLine("Error occurred in processing pending altapay requests. Error : {0}", ex.Message);
        }
        Thread.Sleep(TimeSpan.FromMinutes(3));
    }
}

谢谢

最佳答案

这些步骤将使您达到所需的状态:

  • 将您的方法更改为异步
  • 等待睡眠
  • 使用host.CallAsync()代替host.Call()

  • 我转换了您的代码以反射(reflect)以下步骤。
    static void Main()
    {
        var host = new JobHost();
        host.CallAsync(typeof(Functions).GetMethod("ProcessMethod"));
        // The following code ensures that the WebJob will be running continuously
        host.RunAndBlock();
    }
    
    [NoAutomaticTriggerAttribute]
    public static async Task ProcessMethod(TextWriter log)
    {
        while (true)
        {
            try
            {
                log.WriteLine("There are {0} pending requests", pendings.Count);
            }
            catch (Exception ex)
            {
                log.WriteLine("Error occurred in processing pending altapay requests. Error : {0}", ex.Message);
            }
            await Task.Delay(TimeSpan.FromMinutes(3));
        }
    }
    

    关于c# - 如何使 azure 的webjob连续运行并在没有自动触发的情况下调用public static函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29625813/

    10-11 08:42