0
votes

I'm trying to configure an Azure WebJob with my .NET Core project. Every time I execute the job in Azure, it tells me the error:

Make sure that you are setting a connection string named AzureWebJobsDashboard in your Microsoft Azure Website configuration by using the following format DefaultEndpointsProtocol=https;AccountName=NAME;AccountKey=KEY pointing to the Microsoft Azure Storage account where the Microsoft Azure WebJobs Runtime logs are stored.

Here's my code where I'm configuring everything, as well as the config stuff:

    private static void ConfigureServices(IServiceCollection serviceCollection)
    {
        // Optional: Setup your configuration:
        var configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();

        // add any interfaces that will be needed here
        serviceCollection.AddScoped<IBatchJobService, BatchJobService>();

        // executes the job
        serviceCollection.AddScoped<ExecuteBatchJobs, ExecuteBatchJobs>();

        // One more thing - tell azure where your azure connection strings are
        var connStringDashboard = configuration["ConnectionStrings:AzureWebJobsDashboard"];
        var connStringStorage = configuration["ConnectionStrings:AzureWebJobsStorage"];

        Environment.SetEnvironmentVariable("AzureWebJobsDashboard", connStringDashboard);
        Environment.SetEnvironmentVariable("AzureWebJobsStorage", connStringStorage);
    }

And here's the appsettings.json file:

{
  "ConnectionStrings": {
    "AzureWebJobsDashboard": "DefaultEndpointsProtocol=https;AccountName=xxxxxxxxxxx;AccountKey=mykey;",
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=xxxxxxxxxxx;AccountKey=mykey;"
  }
}

I've set a breakpoint in my code locally on the "var connStringDashboard = ....", and it CORRECTLY reads in the values from the appsettings.json.

Then after that it sets the connection strings via environment variables.

Any thoughts on where I'm going wrong on setting up the connection strings? It seems like Azure's not able to read them from the environment variables for some reason.

1
I would use the CloudConfigurationManager instead which will read configuration file regardless of the environment you're in. So if it's used from the webjobs it will serve you the connectionstring define in the appsettings.json from the main app also. stackoverflow.com/questions/29655648/…Marcus Höglund

1 Answers

5
votes

You need to set the AzureWebJobsDashboard connection string in the portal in your Web App Application Settings blade. The Dashboard runs as a separate site extension and doesn't have access to appsettings.json.

Add the connection string to the connection strings section on the Application Settings blade.

You could change your code so that your connection strings in your appsettings.json file and in Azure's Application Settings are renamed to something else (e.g. WebJobsStorage and WebJobsDashboard), and then it will work.

Environment.SetEnvironmentVariable("AzureWebJobsStorage", configuration.GetConnectionString("WebJobsStorage"));
Environment.SetEnvironmentVariable("AzureWebJobsDashboard", configuration.GetConnectionString("WebJobsDashboard"));

For more details, you could refer to this issue.