skip to Main Content

Is there a special way to define a Key Value setting for ConnectionStrings in Azure App Configuration?

I have tried using:

  • ConnectionStrings:DatabaseKeyName
  • ConnectionStringsDatabaseKeyName

Using the standard builder.Configuration.GetConnectionString("DatabaseKeyName") always results in a null value. Using builder.Configuration["ConnectionStrings:DatabaseKeyName"] also results in null, however if I use a keyname that does not start with ConnectionStrings (e.g. Test:ConnectionStrings:DatabaseKeyName it works as an app setting via builder.Configuration["Test:ConnectionStrings:DatabaseKeyName"]

The Null value for ConnectionStrings:DatabaseKeyName indicates there is some special handling for ConnectionStrings in Azure App Config, but I don’t know where I am going wrong. The Microsoft example pages don’t seem to cover ConnectionStrings (except via KeyVault).

Basically I do not want to have to change this:

services.AddDbContext<IciContext>(o =>
{
    o.UseSqlServer(Configuration.GetConnectionString("DatabaseKeyName"));
});

To this:

services.AddDbContext<IciContext>(o =>
{
    o.UseSqlServer(builder.Configuration["DatabaseKeyName"]);
});

Standard app config connection string setting I need to simulate from Azure App Config:

{
  "ConnectionStrings": {
    "DatabaseKeyName": "Data Source=localhost;Initial Catalog=xxxx;Integrated Security=True"
  },

In my secrets file it is in this format (which does not work with Azure App Config):

{
  "ConnectionStrings:DatabaseKeyName": "Server=xxxx;Database=xxxx;User ID=xxxx;Password=xxxx"
}

2

Answers


  1. To get the Connection String from Azure App Configuration, please check the below process.

    Install the NuGet Package Microsoft.Azure.AppConfiguration.AspNetCore latest version to add the AddAzureAppConfiguration and read the key values.

    To read Azure App Configuration locally, we need to set the secret manager to store the connection string.

    dotnet  user-secrets init
    

    The above command enables the secret storage and sets the secret ID in .csproj of your application.
    enter image description here

    In Program.cs, add the below code

    var myAppConn= builder.Configuration.GetConnectionString("AppConfig");
    
    

    Output:
    enter image description here

    As mentioned in the MSDoc, For the Apps deployed in Azure App Service it is recommended to store Connection String in Configuration Section => Application Settings => Connection Strings of the deployed App.

    enter image description here

    Is there a special way to define a Key Value setting for ConnectionStrings in Azure App Configuration?

    In Azure App Configuration => *YourAppConfiguration* => click on Configuration explorer Under Operations => click on Create => Key-value

    enter image description here

    In Program.cs, add the below code

    var myconn = builder.Configuration.GetConnectionString("AppConfig");
    builder.Host.ConfigureAppConfiguration(builder =>
    {
        builder.AddAzureAppConfiguration(myconn);
    })
                .ConfigureServices(services =>
                {
                    services.AddControllersWithViews();
                });
    

    In any of the cshtml file, add the below code

    @using Microsoft.Extensions.Configuration
    @inject IConfiguration Configuration
    <h1>@Configuration["MyConnection"]</h1>
    

    Output for Key-Value from AppConfiguration:

    enter image description here

    Login or Signup to reply.
  2. Yes, there is a special way to define a key value setting for connection strings in Azure App Configuration. The format of the key for a connection string setting should be:

    ConnectionStrings:<connection-string-name>
    

    Replace with the name of your connection string.

    For example, if you have a connection string named "MyDbConnection" in your Azure App Configuration instance, the key for the setting should be:

    ConnectionStrings:MyDbConnection
    

    To retrieve the connection string in your code, you can use the GetConnectionString() method of the IConfiguration interface:

    var connectionString = configuration.GetConnectionString("MyDbConnection");
    

    Note that the GetConnectionString() method automatically looks for a setting with a key that starts with "ConnectionStrings:". If you use a different prefix, you need to specify the prefix in the key when retrieving the connection string.

    For example, if you define a connection string setting with a key of "Test:ConnectionStrings:MyDbConnection", you can retrieve the connection string as follows:

    var connectionString = configuration.GetConnectionString("Test:ConnectionStrings:MyDbConnection");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search