azure.net-coreazure-app-configuration

Azure App Configuration: Double underscore not equivalent to colon when trying to denote nesting


In Azure App Configuration I have named my settings as so: "Section1__SubSection1__MySetting". When trying to bind configuration to my corresponding Options class in my .NET 6 application, it fails to map correctly (null). If I change the "__" to ":" it works fine.

I'm trying to untangle why/how you can use double underscore to denote nesting, because it is generally recommended to use "__" for more universal support. If I use ":" in my Azure App Configuration keys, would it still work if I run my app on a Linux machine?

Can someone please explain all this?


Solution

  • Written with [StackEdit](>Double underscore not equivalent to colon when trying to denote nesting

    Yes, denoting nested configuration varies depending on the Hosting Environment (Windows/Linux).

    If you want to set the nested configuration in Windows App, then you need to mention the : symbol following the nested key name (which you have already tried).

    You can see that same is mentioned in the MSDoc

    In a default Linux app service or a custom Linux container, any nested JSON key structure in the app setting name like ApplicationInsights:InstrumentationKey needs to be configured in App Service as ApplicationInsights__InstrumentationKey for the key name. In other words, any : should be replaced by __ (double underscore).

    Check the below workaround:

    enter image description here

    and tried to retrieve the values.

    In Program.cs:

    string myconn = builder.Configuration.GetConnectionString("AppConfig");
    //string myconn = builder.Configuration.GetSection("AppConfig").Value;
    builder.Configuration.AddAzureAppConfiguration(myconn);
    

    In Index.cshtml:

    <h4>With Semicolon - @myconfig["Test:Settings:SemiColon"]</h4>
    <h4>With UnderScore - @myconfig["Test__Settings__UnderScore"]</h4>
    

    Local Output:

    enter image description here

    I have deployed the App to Azure Linux App service.

    Deployed App Output:

    enter image description here

    Both : and __ worked for me for the deployed app in Linux App Environment with Azure App Configuration.

    When I tried same with Azure App Settings,

    Local appsettings.json file:

    "Section1": {
      "SubSection1": {
        "MySetting": "Value from Local"
      }
    }
    

    Local Output: enter image description here

    enter image description here

    enter image description here

    enter image description here

    Deployed App Output:

    enter image description here

    If I use ":" in my Azure App Configuration keys, would it still work if I run my app on a Linux machine?