0

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

I have tried using:

  • ConnectionStrings:DatabaseKeyName
  • ConnectionStrings\DatabaseKeyName

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"
}
iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
  • It may be helpful if you can share the complete code. Azure App Configuration doesn't do any special handling. It simply puts each key into `IConfiguration` regardless of how a key is named. – Zhenlan Wang Oct 11 '22 at 01:59
  • @ZhenlanWang the key point is I did not expect to have to change all occurrences of `Configuration.GetConnectionString("MyConnection")`. There should be a way to inject a connection string into Azure App Config (as it seems to treat anything with a "ConnectionString:" prefix differently). I will add code snippets to clarify – iCollect.it Ltd Oct 11 '22 at 10:53

2 Answers2

0

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

Harshitha
  • 3,784
  • 2
  • 4
  • 9
  • I have seen most of that on the Microsoft pages. Most is not relevant to the question. The key part is that you used `Configuration["MyConnection"]` to get the connection string, whereas I would have expected it to support `Configuration.GetConnectionString("MyConnection")` so that existing code does not have to be changed to use a connection via Azure App Config. I have updated question to clarify my aim. – iCollect.it Ltd Oct 11 '22 at 10:56
  • Could you please share your `App Configuration` key value which you have set? – Harshitha Oct 11 '22 at 11:11
  • That is the whole point of my question. What format does the App Configuration key value need to have in Azure App Config to do the same job as a stock-standard `ConnectionStrings` entry? Will post an example of the app config (for what it's worth) – iCollect.it Ltd Oct 11 '22 at 15:53
  • Please confirm whether you want to get the Connection String from Azure App Configuration or Azure App Service Connection String. – Harshitha Oct 12 '22 at 04:46
  • I want to get my database connection strings from Azure App Config, preferably without having to change everywhere they are consumed. Seems like an oversight if you have to rework the setting access just for connection strings. – iCollect.it Ltd Oct 12 '22 at 09:29
  • Please refer [SO Thread](https://stackoverflow.com/a/73906927/19648279). Is this what you are looking for, please let me know. – Harshitha Oct 13 '22 at 11:05
  • That is the starndard way of getting a connection string, yes. For some reason Azure App Config does not appear to be compatible with `GetConnectionString()`. – iCollect.it Ltd Oct 17 '22 at 17:27
  • Yes,`GetConnectionString` works with Azure App Settings. – Harshitha Oct 20 '22 at 08:29
  • I only get back null. Need a working example – iCollect.it Ltd Oct 20 '22 at 13:58
  • @GoneCoding - Did you ever get this figured out? I know exactly what you are saying, and have not figured out how to get it working either. – Brad C. Jan 23 '23 at 17:17
0

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");