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
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 theAddAzureAppConfiguration
and read the key values.To read Azure App Configuration locally, we need to set the secret manager to store the connection string.
The above command enables the secret storage and sets the secret ID in
.csproj
of your application.In Program.cs, add the below code
Output:
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.In Azure App Configuration =>
*YourAppConfiguration*
=> click onConfiguration explorer
Under Operations => click onCreate
=>Key-value
In Program.cs, add the below code
In any of the
cshtml
file, add the below codeOutput for Key-Value from AppConfiguration:
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:
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:
To retrieve the connection string in your code, you can use the GetConnectionString() method of the IConfiguration interface:
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: