0
votes

I'm trying to get double underscore for configurations to work in ASP.Net core 3.1 as described here: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?tabs=basicconfiguration&view=aspnetcore-3.1#environment-variables

I've written some code for configurations that does this manually

  1. check Env Var
  2. check AppSettings...json files

but I saw that in .net 3.1 this is supposed to be supported by default with one caveat around hierarchal settings on unix platforms. In this case you can't use this syntax

Configuration["SomeSection:SomeConfig"]

to resolve this environment variable

## Environment Var
SomeConfig:SomeConfig

Because linux doesn't support colons in Env Var names. This would however work if code was deployed to a windows machine. So instead .net seems to indicate you can use this syntax

Configuration["SomeSection__SomeConfig"]

which will resolve any of the following configurations

// AppSettings...json
{
   "SomeSection" {
      "SomeConfig": "some value"
   }
}

or

# Environment Var on linux
SomeConfig__SomeConfig

or

// Environment Var on windows
SomeConfig:SomeConfig

Does anyone know what I'm doing wrong?

Edit: Note that I know the answer to this questions and will happily answer it if it's is reopened.

1
It isn't fully clear, which problem do you have right now and what is wrong with code abovePavel Anikhouski
Configuration["SomeSection:SomeConfig"] doesn't access an environment variable, it retrieves the SomeConfig setting from the SomeSection section, no matter where this comes from. The values are provided by config providers that may load the settings from files, databases, the command line, services etc. So what you seem to ask is only how the environment variable provider works in Linux and how env variable names are mapped to configuration settingsPanagiotis Kanavos
According to the docs you link to, the environment variable named SomeSection__SomeConfig should work on all platformsPanagiotis Kanavos

1 Answers

3
votes

I was miss understand documentation. The way it work is if you use this code in your start.cs

Configuration["SomeSection:SomeConfig"]

it will correctly resolve

// AppSettings...json
{
   "SomeSection" {
      "SomeConfig": "some value"
   }
}

from your various AppSettings files.

However, if you define an environment variable with the name SomeSection:SomeConfig that will be used instead of the AppSettings value which is a great feature. This is often desirable when running the app in a docker container.

However, if are on a linux operation system it's not possible to create an environment variable with the name SomeSection:SomeConfig. So if you are on linux machine or in a linux container, instead if you name your environment variable SomeSection__SomeConfig with a __ (double underscore) instead of a : then it will be resolved by

Configuration["SomeSection:SomeConfig"]