Retrieving Connection Strings for Web Applications using ConfigurationBuilder

We keep some of our sensitive keys and connection strings in the Connection Strings section of the web app's app settings:

Azure Web App connection strings

We retrieve the configuration settings with ConfigurationBuilder

:

Configuration = new ConfigurationBuilder()
    .SetBasePath(environment.ContentRootPath)
    .AddEnvironmentVariables()
    .Build();

      

I would have expected to AddEnvironmentVariables()

take these connection strings, but they are not. Note that this works if you set these values ​​as "Application Settings" in the web application.

Upon closer inspection (using the Kudu console), I found that the environment variables set for these connection strings have CUSTOMCONNSTR_ prefixed with the key name:

CUSTOMCONNSTR_MongoDb:ConnectionString=...
CUSTOMCONNSTR_Logging:ConnectionString=...
CUSTOMCONNSTR_ApplicationInsights:ChronosInstrumentationKey=...

      

How do I now read in these connection lines using ConfigurationBuilder

?

EDIT:

I found that a convenience overload AddEnvironmentVariables

exists with a parameter prefix

described as:

//   prefix:
//     The prefix that environment variable names must start with. The prefix will be
//     removed from the environment variable names.

      

But adding .AddEnvironmentVariables("CUSTOMCONNSTR_")

a config to the constructor doesn't work either!

+3


source to share


2 answers


But adding .AddEnvironmentVariables ("CUSTOMCONNSTR_") to the config constructor doesn't work either!

The prefixed AddEnvironmentVariables simply adds a constraint on environment variables that must be prefixed. It won't change environment variables.

To get the value from the connection string configuration, you can use the following code.

Configuration.GetConnectionString("MongoDb:ConnectionString");

      



To customize the hierarchical structure, add it to your application settings instead of connection strings in the Azure portal.

How do I now read in these connection lines using the ConfigurationBuilder?

As a workaround, you can re-add the EnvironmentVariable and rebuild the ConfigurationBuilder after getting the connection string. The code below is for your reference.

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables();

    Configuration = builder.Build();
    //Add EnvironmentVariable and rebuild ConfigurationBuilder
    Environment.SetEnvironmentVariable("MongoDb:ConnectionString", Configuration.GetConnectionString("MongoDb:ConnectionString"));
    builder.AddEnvironmentVariables();
    Configuration = builder.Build();
}

      

+1


source


It should just work, and it does for me in my sample app: https://github.com/davidebbo-test/AspNetCoreDemo . In particular:

  • MyDatabase

    the connection string is defined here .
  • Used here here .
  • If you define the MyDatabase

    conn string in the Azure Portal, you will see the new value at runtime (go to the About page).


So, start by checking that my work is working and try to figure out what you can do differently. You don't need to make any prefix assumptions CUSTOMCONNSTR_

!

+1


source







All Articles