.NET Core 配置管理器,读取一个键里面有一个冒号的字典,给出一个关于值的奇怪的错误消息,这是预期的吗? [英] .NET Core configuration manager, reading a dictionary where one key has a colon inside, gives an odd error message about the value, is this expected?

查看:19
本文介绍了.NET Core 配置管理器,读取一个键里面有一个冒号的字典,给出一个关于值的奇怪的错误消息,这是预期的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下程序:

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 文件时,运行完美:

When given this appsettings.json file, runs perfectly:

{
    "Values": {
        "a": true,
        "b": false
    }
}

但将 appsettings.json 内容更改为:

{
    "Values": {
        "a:b": true,
        "b": false
    }
}

我得到这个异常:

未处理的异常:System.InvalidOperationException:无法创建System.Boolean"类型的实例,因为它缺少公共无参数构造函数.

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:DevConsoleApp3ConsoleApp3Program.cs:line 15

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

What am I doing wrong? Note that having a colon in the key is perfectly legal json, but perhaps storing any odd dictionary in the appsettings.json file is not supported?

推荐答案

参考 ASP.NET Core 中的配置:分层配置数据

配置 API 能够通过在配置键中使用分隔符扁平化分层数据来维护分层配置数据.

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 尝试绑定 Dictionary 属性在 Options

还引用了这个 GitHub 问题

冒号是为键中的特殊含义保留的,因此不应将它们用作普通键值的一部分.

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

这篇关于.NET Core 配置管理器,读取一个键里面有一个冒号的字典,给出一个关于值的奇怪的错误消息,这是预期的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆