简体   繁体   中英

Azure not reading connection string from WebJob

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.

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 (eg 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM