gpt4 book ai didi

c# - .NET Core 配置管理器,读取一个键里面有一个冒号的字典,给出一个关于值的奇怪的错误消息,这是预期的吗?

转载 作者:行者123 更新时间:2023-12-04 12:42:51 24 4
gpt4 key购买 nike

以下程序:

using System;
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;

namespace ConsoleApp3
{
class Program
{
static void Main()
{
var builder = new ConfigurationBuilder();
builder.AddJsonFile("appsettings.json");

var configuration = builder.Build();
var options = configuration.Get<Options>();
foreach (var kvp in options.Values)
Console.WriteLine($"{kvp.Key}: {kvp.Value}");
}
}

internal class Options
{
public Dictionary<string, bool> Values { get; } = new Dictionary<string, bool>();
}
}

当给出这个 appsettings.json文件,完美运行:
{
"Values": {
"a": true,
"b": false
}
}

但更改 appsettings.json内容:
{
"Values": {
"a:b": true,
"b": false
}
}

我得到这个异常(exception):

Unhandled Exception: System.InvalidOperationException: Cannot create instance of type 'System.Boolean' because it is missing a public parameterless constructor.



堆栈跟踪:
   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.BindProperty(PropertyInfo property, Object instance, IConfiguration config, BinderOptions options)
at Microsoft.Extensions.Configuration.ConfigurationBinder.BindNonScalar(IConfiguration configuration, Object instance, 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)
at ConsoleApp3.Program.Main() in D:\Dev\ConsoleApp3\ConsoleApp3\Program.cs:line 15

我究竟做错了什么?请注意,键中有一个冒号是完全合法的 json,但也许不支持在 appsettings.json 文件中存储任何奇怪的字典?

最佳答案

引用 Configuration in ASP.NET Core: Hierarchical configuration data

The Configuration API is capable of maintaining hierarchical configuration data by flattening the hierarchical data with the use of a delimiter in the configuration keys.

When the file is read into configuration, unique keys are created to maintain the original hierarchical data structure of the configuration source. The sections and keys are flattened with the use of a colon (:) to maintain the original structure



这意味着在以下 appsettings.json 文件中
{
"Values": {
"a:b": true,
"b": false
}
}

键将被展平为
  • 值:a:b
  • 值:b

  • ConfigurationBinder.BindDictionary 时,这将破坏设置文件的结构尝试绑定(bind) Dictionary<string, bool>属性(property)在 Options
    还引用此 GitHub Issue

    Colons are reserved for special meaning in the keys, so they shouldn't be used as part of normal key values.

    关于c# - .NET Core 配置管理器,读取一个键里面有一个冒号的字典,给出一个关于值的奇怪的错误消息,这是预期的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52121374/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com