The code behavior of configuration has changed when upgrade from .NET Core 2.x to .NET Core 3.1

Tony Wei 1 Reputation point
2021-10-27T04:34:13.607+00:00

If there is a config section like below in AppSetting.json and the APP runtime is .NET Core 2.x, no exception occurred and the value of AllowedHosts should be the last one, but when I upgrade to .NET Core 3.1, the exception occurred.

{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"AllowedHosts": "10.0.1.2",
"test": {
"dd": 12
}
}

144008-screenshot-2021-10-27-112037.png

I have checked he document from Microsoft site, per the document below, the code behavior should be same between .NET Core 2.x and .NET Core 3.1

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1#configuration-keys-and-values-1

Configuration keys and values
Configuration keys:

Are case-insensitive. For example, ConnectionString and connectionstring are treated as equivalent keys.
If a key and value is set in more than one configuration providers, the value from the last provider added is used. For more information, see Default configuration.
Hierarchical keys
Within the Configuration API, a colon separator (:) works on all platforms.
In environment variables, a colon separator may not work on all platforms. A double underscore, __, is supported by all platforms and is automatically converted into a colon :.
In Azure Key Vault, hierarchical keys use -- as a separator. The Azure Key Vault configuration provider automatically replaces -- with a : when the secrets are loaded into the app's configuration.
The ConfigurationBinder supports binding arrays to objects using array indices in configuration keys. Array binding is described in the Bind an array to a class section.
Configuration values:

Are strings.
Null values can't be stored in configuration or bound to objects.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,189 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,279 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,125 questions
{count} votes