Search code examples
c#azureconnection-stringazure-storageazure-webjobs

What is the recommended place to store Azure Storage Connection strings


When creating a new WebJob project, the AzureWebJobsStorage connection string is added to App.config > configuration > connectionStrings

In contrast, the microsoft documentation for storage account connection strings clearly states AppSettings is where they'd put it.

What is the recommended place? Is connectionStrings only for database connection strings?

We will consume the connection string in an Azure Web App


Solution

  • When creating a new WebJob project, the AzureWebJobsStorage connection string is added to App.config > configuration > connectionStrings

    When your creating the Azure WebJob project, it would reference the related WebJob libraries (Microsoft.Azure.WebJobs, Microsoft.Azure.WebJobs.Core). You need to specify a storage account for the WebJob SDK to log traces and metric data. And the connection string need to be specified under the connectionStrings section of your config file, otherwise you would retrieve the following error:

    Microsoft Azure WebJobs SDK Dashboard connection string is missing or empty. The Microsoft Azure Storage account connection string can be set in the following ways:

    1. Set the connection string named 'AzureWebJobsDashboard' in the connectionStrings section of the .config file in the following format

    2. Set the environment variable named 'AzureWebJobsDashboard'

    3. Set corresponding property of JobHostConfiguration

    Per my understanding, the Azure WebJob SDK only support reading the storage connection string from the above approaches. You could also set the connection string under the appSettings section, at this point you need to specify the related properties when constructing the JobHostConfiguration as follows:

    static void Main()
    {
        var config = new JobHostConfiguration()
        {
            DashboardConnectionString= ConfigurationManager.AppSettings["AzureWebJobsDashboard"],
            StorageConnectionString= ConfigurationManager.AppSettings["AzureWebJobsStorage"]
        };
    
        if (config.IsDevelopment)
        {
            config.UseDevelopmentSettings();
        }
    
        var host = new JobHost(config);
        // The following code ensures that the WebJob will be running continuously
        host.RunAndBlock();
    }
    

    Moreover, you could Use the Azure storage emulator for development and testing. While for production, you could specify the related app settings or connection strings on Azure Portal for overriding your dev settings. More details, you could refer to here for a better understanding of it.

    What is the recommended place? Is connectionStrings only for database connection strings?

    Per my understanding, when you are using the 3rd party libraries, you need to follow it's instruction for configuration. While you writing your code, you could define the connection string as you wish and read them in the corresponding way.