Search code examples
c#azureazure-webjobsazure-webjobssdk

Running web job gives ArgumentNullException on start


I'm developing an Azure webjob using .NET Framework but when I'll run this localy, I've this exception after the startup.

Microsoft.Azure.WebJobs.Host.Listeners.FunctionListenerException: The listener for function [Name of function] was unable to start.

Inner exception:

ArgumentNullException: Value cannot be null.
Parameter name: connectionString

This is the code inside the Program class of the web job.

static void Main()
{
    HostBuilder builder = new HostBuilder();

    builder.ConfigureWebJobs(b =>
    {
        b.AddAzureStorageCoreServices();
        b.AddTimers();
    });

    using (IHost host = builder.Build())
    {
        host.Run(); // <-- error happens on this line
    }
}

Inside the App.config I've added next two connection strings:

<add name="AzureWebJobsDashboard" connectionString="DefaultEndpointsProtocol=https;AccountName=[Name];AccountKey=[Key]" />
<add name="AzureWebJobsStorage" connectionString="DefaultEndpointsProtocol=https;AccountName=[Name];AccountKey=[Key]" />

With [Name] and [Key] as the account name en key from a live environment.

Also when I change the connection strings to UseDevelopmentStorage=true, I'm getting the same exception.

How could I solve this?


Solution

  • Update:

    If you're using Azure WebJobs SDK of version 3.x with .NET Framework, there is an issue: Version 3.x is incompatible with .NET Framework and PackageReference.

    So there're several ways as workaround:

    1. Directly use the Azure WebJobs template from Visual studio, it's based on .net framework.

    2. You can add an appsettings.json file (Note: right click the file → select Properties → then set "Copy to Output Directory" to "Copy if newer") to the .NET Framework project, it will work(based on 1, it will throw errors, but it can work). The JSON file looks like below:

      {
          "AzureWebJobsStorage": "{storage connection string}"
      }
      
    3. The best solution is to use .NET Core with WebJobs SDK of version 3.x.


    Original answer:

    If you're using WebJob SDK version 3.x, I suggest you should create a .NET Core console project instead of .NET Framework console project, then you should follow this official doc.

    First, create a .NET Core console app. And install all the necessary packages mentioned in the official doc.

    Your program.cs:

    static void Main()
    {
        HostBuilder builder = new HostBuilder();
    
        builder.ConfigureWebJobs(b =>
        {
            b.AddAzureStorageCoreServices();
            b.AddAzureStorage();
            b.AddTimers();
        });
    
        using (IHost host = builder.Build())
        {
            host.Run();
        }
    }
    

    Then create a function, please refer to this section.

    Then add an appsettings.json file (Note: right click the file → select Properties → then set "Copy to Output Directory" to "Copy if newer"), add the AzureWebJobsStorage inside it:

    {
        "AzureWebJobsStorage": "{storage connection string}"
    }
    

    Please let me know if you still have more issues about that.