Asp.Net Core自定义转换器为IDictionary设置JSON

问题描述 投票:0回答:1

我想读取appsettings文件,但是当将一个属性定义为IDictionary时出现问题:将值转换为字符串。

例如,我在定义的设置中:

  "Queue":{ 
            "Name": "Test1",
            "Durable": true, 
            "Exclusive": false, 
            "AutoDelete": false, 
            "Arguments": {
              "x-expires": 10000,
              "x-overflow": "drop-head",
            }
 },

用于绑定的类定义为

public class QueueSettings
    {
        public string Name { get; set; } = "";
        public bool Durable { get; set; } = true;
        public bool Exclusive { get; set; } = false;
        public bool AutoDelete { get; set; } = false;

        public IDictionary<string, object> Arguments{ get;  set; }
    }

绑定完成为

QueueSettings queueSettings = new QueueSettings();
settings.GetSection("Queue").Bind(queueSettings);

在此示例中queueSettings.Arguments [“ x-expires”]是字符串(“ 10000”),但我希望将其作为整数。如何在Net Core服务集合中设置JsonConverter?我了解它不使用NewtonJson,因此JsonConverter属性没有帮助。

.net-core application-settings jsonconverter
1个回答
0
投票

您可以根据需要手动绑定。

services.Configure<QueueSettings>(configs =>
{
    configs.Name = configuration.GetSection("Queue").GetValue<string>("Name");
    configs.Arguments.Add("x-expires",  configuration.GetSection("Queue:Arguments").GetValue<int>("x-expires"));
});
© www.soinside.com 2019 - 2024. All rights reserved.