Home > Back-end >  Override ASP.NET Core appsettings key name that as dots with environment variable in a container
Override ASP.NET Core appsettings key name that as dots with environment variable in a container

Time:11-29

I'm trying to migrate a legacy app into a new .net 6 version, the issue that I have is that this app has a 3rd party library with keys that will be looked up in the appsettings.json file.

Something like this(note the dots in the key):

{
    "one.special.key":"one value"
}

The issue that I'm facing now is that my new app will be running inside a container and the keys will be injected using environment variables and I don't think that containers environments (aka - linux) accept environment variables with dots, only the convention with one/double underscore like this: one_special_key.

How can I override an appsetting.json that has a key with dots in it like some.key.with.dots=hello instead of the traditional some_key_without_dots=hello?

CodePudding user response:

If I am not wrong - if you keep an environment variable like this one__special__key - the application will use this value instead of the value from the appsettings.json file.

From the documentation

Using the default configuration, the EnvironmentVariablesConfigurationProvider loads configuration from environment variable key-value pairs after reading appsettings.json, appsettings.{Environment}.json, and user secrets. Therefore, key values read from the environment override values read from appsettings.json, appsettings.{Environment}.json, and user secrets.

CodePudding user response:

Found the answer in this k8s PR: https://github.com/kubernetes/kubernetes/pull/48986 the validation was made more permiss and it allows to pass dot based environment variables names.

  • Related