Overriding an Array Configuration Object

Surprise! The arrays are not replaced as a whole.

Changhui Xu
Level Up Coding
Published in
4 min readFeb 15, 2021

--

ASP.NET Core uses configuration providers to read configuration key-value pairs from a variety of sources. You can read more about configuration in ASP.NET Core in its official documentation, which has covered tons of use cases in detail.

Recently, when I set a JSON array in the appsettings.json files in an ASP.NET Core project, the result of the final configuration data surprised me. The array did not behave the same as other JSON objects that could be overridden in an environment-specific appsettings.json file. Instead, the final array could have more elements than expected. After some googling, I found that there are already many Stack Overflow questions (1, 2, 3, 4, and 5) and GitHub issues (1, 2, 3, 4, 5, and 6) discussing this interesting behavior of configuration inheritance for arrays. I think it is worth writing a short article to describe the “problem” so that you won’t stumble over the same stone.

What’s the “Problem”

A well-known feature in ASP.NET Core configuration is that if there are some key-value pairs with the same keys in different configuration sources, then a configuration provider that is added later will override previous key settings. For example, a connection string set in the…

--

--

Lead Application Developer. MBA. I write blogs about .NET, Angular, JavaScript/TypeScript, Docker, AWS, DDD, and many others.