15

I want to create a continuously running WebJob but first I want to try and run it locally for debugging. I am using Visual Studio 2015 and I have the Azure storage emulator running (I can run the sample for Azure WebJobs in visual studio).

When I run the below it fails on the new JobHost() line with:

Exception: Value cannot be null. Parameter name: method

    static void Main()
    {
        var host = new JobHost();
        host.CallAsync(typeof(Functions).GetMethod("GetNextJob"));
        // The following code ensures that the WebJob will be running continuously
        host.RunAndBlock();
    }
    [NoAutomaticTriggerAttribute]
    public static async Task GetNextJob()
    {
        while(true)
        {
            try
            {
                var log = new Logger();
                log.WriteInfo("Getting next job to be run", 0, 0, "Brain");
                //Console.WriteLine("Getting new Job and putting to que");
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            await Task.Delay(TimeSpan.FromSeconds(5));
        }
    }

Can I even run the continous running jobs locally?

d219
  • 2,707
  • 5
  • 31
  • 36
DanScan
  • 831
  • 2
  • 10
  • 23
  • Question: Wouldn't it be a more flexible way to control the frequency of the method call, to remove the while loop and the sleep timer and then set it up with a cron expression in the Azure dashboard? Then you could control it without re deploying the code – Jepzen Mar 16 '18 at 10:00

1 Answers1

18

Azure WebJobs are typically just console applications. You can run it locally just like you would debug, test and run any other console application. I'd recommend getting the Azure WebJobs SDK and running through the tutorial Create a .NET WebJob in Azure App Service.

d219
  • 2,707
  • 5
  • 31
  • 36
viperguynaz
  • 12,044
  • 4
  • 30
  • 41
  • 1
    for the sake of completeness, it's not quite true. Azure web job environment is different from your local PC's. Almost all shared resources (registry, graphics, etc) are not accessible: https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox – avs099 Oct 21 '18 at 13:49
  • Both links now points to the project WiKi. Here is another one which points to the docs and explain how to run the job locally: https://learn.microsoft.com/pt-br/azure/app-service/webjobs-sdk-get-started – Ulysses Alves Aug 06 '19 at 20:17
  • The link by @UlyssesAlves takes you to the a Brazilian Portugese page - here's the English equivalent https://learn.microsoft.com/en-gb/azure/app-service/webjobs-sdk-get-started and this may also be useful https://learn.microsoft.com/en-us/azure/app-service/webjobs-create – d219 Apr 23 '20 at 12:30
  • @avs099 the difference is hardware it is running in, for rest it is pretty much console application and uses Azure Storage to sync jobs. Webjobs running at cloud/Azure does not share local resources. – Teoman shipahi Dec 01 '20 at 05:24