Azure WebJob 不会在调试器中本地 运行

Azure WebJob won't run locally in debugger

我的 Azure WebJob 曾经在 VS2015 Debugger 中 运行,但我发现它逐渐变得非常断断续续,现在完全不会 运行。如果我将它部署到 Azure,它工作正常。该作业被标记为 RunOnStartUp。

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

public class TestJob : BaseJob
{
    public static async Task StartupJob([TimerTrigger("05:00:00", RunOnStartup = true)] TimerInfo timerInfo, TextWriter log)
    {
        log.WriteLine("StartupJob");
        await Jobs.Test(some params);
        log.WriteLine("Sorted");
    }
}

我需要做什么才能在调试器中获得它运行?

我猜你在 Azure 中和在本地调试时使用相同的存储帐户?如果是这种情况 - TimeTrigger 作为单例运行,这意味着它需要获取锁才能执行。如果您的 webjob 在 Azure 中已经 运行,则您尝试调试的本地版本无法获取锁。

为避免这种情况,只需为 "live" Azure 版本和本地本地开发使用不同的存储帐户。

我还建议在本地调试时启用 "development settings" - config.UseDevelopmentSettings();。如果启用它,您将看到消息 "unable to acquire lock for function..."(或类似内容)。

请参阅 Jason Haley 在 this 主题中的评论:

(total hack but should work) rename the function while debugging so the lock listener blob name will be different.

这个 hack 对我有用。也许为了让它不那么骇人听闻,您可以使用 Disable-attribute 创建一个定时器触发的功能,该功能只会在您的本地环境中启用:

  1. 创建处理逻辑的“MyFunction”。这是将 运行 在你的 Azure 应用程序中的那个。注意 docs.

    建议的 RunOnStartup=false
     [FunctionName("MyFunction")]
     public async Task RunJob(
         [TimerTrigger("0 0 0 * * *", RunOnStartup = false)] TimerInfo timer)
     {
        //logic here
     }
    
  2. 使用 Disable 属性和不同的方法名称创建“MyFunction-Local”。所有这一切都是调用上面的方法。

     [FunctionName("MyFunction-Local")]
     [Disable("Disable_MyFunction")]
     public async Task RunJobLocal(
         [TimerTrigger("0 0 0 * * *", RunOnStartup = true)] TimerInfo timer)
     {
        RunJob(timer);
     }
    
  3. 在您的本地应用程序配置中,设置 {"Disable_MyFunction" = false},而对于 Azure 中的应用程序 运行ning,将其设置为 true。