在本地调试 Azure WebJob

Debug Azure WebJob locally

我一直在创建一个 Azure WebJob,它显然工作得很好,但我需要创建一个新函数,并且我需要在本地测试才能上传到生产站点,我 运行 调试控制台程序和这个识别所有功能,但我无法触发任何功能。

文档说下一次触发是每分钟.... (https://github.com/Azure/azure-webjobs-sdk-extensions#timertrigger)

我的代码:

public static async void ProcessAugustEndowments([TimerTrigger("0 */1 * * * *", RunOnStartup = true)] TimerInfo timerInfo)
    {
        Console.WriteLine("Endowments process tried");

        await endowmentNotification();
    }

输出:

I run on Debug the console program and this recognize all functions but I can't trigger any function.

根据你的代码,我在我这边测试了一下,发现当我第一次调试应用程序时,我可以得到以下输出:

但是,当我重新启动应用程序时,我发现该功能需要一些时间才能被触发。

请确保您已经安装了"Microsoft.Azure.WebJobs"和"Microsoft.Azure.WebJobs.Extensions"的最新版本包。更多详情,可以关注这个tutorial.

并且请尽量缩短您TimerTrigger中的时间间隔,等待job host启动一段时间后,再尝试看看是否可以在您这边触发该功能。这是我的代码示例,您可以参考它。

Program.cs

static void Main()
{
    JobHostConfiguration config = new JobHostConfiguration();
    // Add Triggers and Binders for Timer Trigger.
    config.UseTimers();
    JobHost host = new JobHost(config);
    //host.RunAndBlock();
    host.Start();
    Console.WriteLine("[{0}] Job Host started!!!", DateTime.Now);
    Console.ReadLine();
}

Function.cs

//Function triggered by a timespan schedule every 5 sec.
public static async void ProcessAugustEndowments([TimerTrigger("*/5 * * * * *", RunOnStartup = true)] TimerInfo timerInfo)
{
    Console.WriteLine("Endowments process tried");
    await endowmentNotification();
}

private static async Task endowmentNotification()
{   
    //sleep for 2 sec to simulate processing business logic
    await Task.Delay(TimeSpan.FromSeconds(2));
}

另外,如果TimerTrigger不能满足你的要求,你可以参考这位官方tutorial to create a schedule WebJob, also you could refer to this blog

如果你的函数没有被触发,我认为是因为你没有配置你的 JobHost 你使用 TimerTrigger:

var config = new JobHostConfiguration();
config.UseTimers();

JobHost 如何与触发器一起工作:

  • JobHost 启动时,它会发现并索引带有一些 *TriggerAttribute 的函数。

  • 通过指定 config.UseTimers(); 告诉 JobHost 使用 TimerTriggerAttribute.

  • 索引函数

这也适用于其他类型的触发器,例如 ServiceBusTrigger,需要 config.UseServiceBus() 才能工作