

Discover more from Ahmadali’s Substack
Overwriting Configuration using Environmental Variables in ASP.NET Core
I’m developing a dockerized system and I want environmental variables in my docker container overwriting my appsettings.json to make my app…
This is a cross-post of my Medium post.
I’m developing a dockerized system and I want environmental variables in my docker container overwriting my appsettings.json
to make my app easier to config (otherwise I needed to copy the new configuration to the container which made it incompatible with swarm or kubernetes or add volume for the config file).
I searched for ways to overwrite configs but my search results were talking about ways to configure app and using IOption
and the fact that you can inject configurations from many sources including environmental variables and command line arguments
I also find out that configurations with the same name are over-writable by order of adding configuration provider so based on default provider adding order (aka. when you use WebHost.CreateDefaultBuilder(args)
in your Program.cs
) configs in appsettings.json
will be overwritten by configs with the same name in appsettings.development.json
if you run your app in development mode.
So now I have to find a way to overwrite my configs using an environmental variable with the same name. I found out that in some systems : can be part of the name of environmental variable so now I can have an environmental variable named section:subsection
overriding section.subsection
of my appsettings.json
. But what about systems that don’t allow : in environmental variable name? I found out here that dotnet core has this convention that __ (double underscore) can replace : so section__subsection
environmental variable overwrites section:subsection
.
voila! we have the environmental variable config overwriting system completely supported out of the box by dotnet core configuration system :)