Search code examples
azureazure-webjobs

Azure WebJob won't run locally in debugger


My Azure WebJob used to run in the VS2015 Debugger, but I found it gradually became very intermittent and now won't run at all. It works fine it I deploy it to Azure. The job is marked as 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");
    }
}

What do I need to do to get it running in the Debugger?


Solution

  • I'm guessing you use the same storage account for your job in Azure and when you debug it locally? If that's the case - the TimeTrigger runs as a singleton which means it needs to acquire a lock to be able to execute. If your webjob is already running in Azure your local version, which you're trying to debug, is not able to acquire the lock.

    To avoid this just use different storage accounts for "live" Azure version and local local development.

    I would also recommend to enable "development settings" - config.UseDevelopmentSettings(); - when you debug locally. If you enable it you'll see the messages "unable to acquire lock for function..." (or something similar).