You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Create a .NET Core console app project with the following appsettings.json contents:
{
"Values": {
"key": "value"
}
}
Add NuGet references to:
Microsoft.Extensions.Configuration.Binder v2.1.1
Microsoft.Extensions.Configuration.Json v2.1.1
Run the following:
class Program
{
static void Main()
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var d = configuration.GetSection("Values").Get<Dictionary<string, string>>();
}
}
The result, d, is a dictionary with one pair, as expected.
However, replacing "key" with "k:ey" in the config and running again causes the following exception:
System.InvalidOperationException
HResult=0x80131509
Message=Cannot create instance of type 'System.String' because it is missing a public parameterless constructor.
Source=Microsoft.Extensions.Configuration.Binder
StackTrace:
at Microsoft.Extensions.Configuration.ConfigurationBinder.CreateInstance(Type type)
at Microsoft.Extensions.Configuration.ConfigurationBinder.BindInstance(Type type, Object instance, IConfiguration config, BinderOptions options)
at Microsoft.Extensions.Configuration.ConfigurationBinder.BindDictionary(Object dictionary, Type dictionaryType, IConfiguration config, BinderOptions options)
at Microsoft.Extensions.Configuration.ConfigurationBinder.BindInstance(Type type, Object instance, IConfiguration config, BinderOptions options)
at Microsoft.Extensions.Configuration.ConfigurationBinder.Get[T](IConfiguration configuration, Action`1 configureOptions)
...
Other info
I know : is a reserved character for IConfiguration paths, so maybe this isn't fixable, although is there not some way the : could be escaped when reading from JSON configuration sources?
Minimal working example
Create a .NET Core console app project with the following
appsettings.json
contents:Add NuGet references to:
Microsoft.Extensions.Configuration.Binder
v2.1.1Microsoft.Extensions.Configuration.Json
v2.1.1Run the following:
The result,
d
, is a dictionary with one pair, as expected.However, replacing
"key"
with"k:ey"
in the config and running again causes the following exception:Other info
I know
:
is a reserved character forIConfiguration
paths, so maybe this isn't fixable, although is there not some way the:
could be escaped when reading from JSON configuration sources?I was following someone's example in https://stackoverflow.com/questions/42846296/how-to-load-appsetting-json-section-into-dictionary-in-net-core, and stumbled across this issue by accident (some of my
Dictionary
keys contain colons).The text was updated successfully, but these errors were encountered: