0

We have an .NET core app hosted in azure. Some of the configurations from appsettings.json are overridden in azure under app service / configuration / application settings. This works fine for "flat" properties, but not for arrays (of flat values), which is explicitly documented by microsoft here (also referenced in this answer).

For example, for the following section from the appsettings:

"Config": {
    "SomeProperty": "Some string value",
    "SomeArray": [
        "Apple",
        "Banana",
        "Orange"
    ]
}

Setting Config:SomeProperty = new string correctly changes the property thus environment variables seem to be set correctly in general. I would like to override the entire array Config:SomeArray in the azure appsettings. None of the following options changes anything about the array value at runtime:

Key Value
Config:SomeArray ['NewValue']
Config:SomeArray ["NewValue"]
Config:SomeArray:0 NewValue

The last option comes from multiple answers around the web (e.g. here) which state that one could'nt override a whole array (of subobjects) but only element-wise.

Am I doing something wrong or is it possible to change array properties over azure application settings? If not (or if only elementwise, somehow), is there any better option than changing it to a string property and parsing the array manually (the only alternative I see right now, which people obviously (and rightfully) dislike) ?

T_01
  • 1,256
  • 3
  • 16
  • 35

1 Answers1

-1

As you mentioned, Azure App Service Configuration UI does not support overriding array values in appsettings.json. However, you can work around this limitation by using a string to represent the array in the Azure configuration, and then parsing it in your .NET code to get the array.

You can check below approach.

In the Azure Configuration UI, add a new setting with the key "Config:SomeArray" and a string value that represents the array, such as "Apple,Banana,Orange". In your .NET code, retrieve the value of "Config:SomeArray" from the configuration, and parse it into an array:

"Config": {  
"SomeProperty": "Some string value",  
"SomeArray": "Apple,Banana,Orange"  
}
 string containerName = Environment.GetEnvironmentVariable("SomeArray");
 string[] someArray = containerName.Split(',').ToArray();
     foreach (var item in someArray)
      {
          Console.WriteLine(item);
      }

This code uses the Split() method to split the string into an array of strings using the comma as a delimiter, and then uses the ToArray() extension method to convert the result to a string array.

You can now use the parsed array in your code:

foreach (string fruit in someArray)
{
    Console.WriteLine(fruit);
}

enter image description here

Use environment variables: Define your array as an environment variable, and retrieve it in your code. For example, you can define an environment variable called SOME_ARRAY with the value Apple,Banana,Orange. In your code, you can retrieve this environment variable and split it into an array.

Use Azure Key Vault: Store your array in Azure Key Vault and retrieve it in your code. Azure Key Vault allows you to store and manage secrets, such as passwords, keys, and certificates. You can store your array as a secret in Azure Key Vault and retrieve it in your code.

Updated

The app setting "MY_ENV_VAR" has a value of a list with three entries: "entry1", "entry2", and "entry3". If your application needs to use this value as a list, you will need to parse it into a list data structure first.


string envVarValue = Environment.GetEnvironmentVariable("MY_ENV_VAR");
List<string> myList = new List<string>();

if (!string.IsNullOrEmpty(envVarValue))
{
       string[] entries = envVarValue.Replace("[", "").Replace("]", "").Split(',');
    
    foreach (string entry in entries)
    {
        string trimmedEntry = entry.Trim();
        if (!string.IsNullOrEmpty(trimmedEntry))
        {
            myList.Add(trimmedEntry);
        }
    }
}
foreach (string entry in myList)
{
    Console.WriteLine(entry);
}
Rajesh Mopati
  • 1,329
  • 1
  • 2
  • 7
  • Obviously, as I mentioned myself in the question, I can create a string property myself and parse it. This is not what I asked for. Also, azure configuration properties are injected into the application as environment variables. – T_01 Mar 21 '23 at 23:16
  • You can use the Azure CLI or Azure PowerShell to set the app settings in bulk. You can also use the Azure portal to edit the app settings in bulk. And, it is not possible to change the array properties in Azure appsettings. If you want to change the array properties, you may need to change it to a string property and parse the array manually. For more information refer to [MSDoc](https://github.com/MicrosoftDocs/azure-docs/blob/main/articles/app-service/configure-common.md). – Rajesh Mopati Mar 22 '23 at 02:06
  • I already read the documentation. There I Found the mentioned [entry about array](https://learn.microsoft.com/en-us/azure/app-service/configure-common?tabs=portal#configure-arrays-in-app-settings). This is why I even asked this question. – T_01 Mar 22 '23 at 09:42
  • In the answer, I just added information from Updated. – Rajesh Mopati Mar 22 '23 at 10:47