来自appsettings.json的配置类

时间:2017-11-29 22:08:01

标签: c#

我正在尝试构建要从appsettings.json加载的配置的类。

这是我遇到问题的appsettings.json部分。我无法弄清楚如何为ConnectionStrings构建对象。 下面的示例不允许多个连接字符串,我们也不希望" name"基于连接的属性 appsettings.json中的字符串名称。

据我所知,我们可能需要更改引用连接字符串的代码,以便以不同于当前的方式读取此对象。

任何人都有一种简单的方法可以做到这一点吗?

appsettings.json

"ModuleConfig": {
    "IpAddress": "The IP",
    "Port": "The Module Port",
    "Buffer": "1024",
    "ConnectionStrings": {        
        "OracleKeyManagement": "The Connection string"
    },
    "Redis": {
        "Host": "localhost",
        "Port": "The Redis Port"
    }
}

配置类 - 已更新

public class ModuleConfig
{
    IpAddress { get; set; } = "The IP",
    Port { get; set; } = "The Module Port",
    Buffer { get; set; } = "1024",
    public Dictionary<string,string> ConnectionStrings { get; set; }
    public RedisConfig RedisConfig { get; set; } 
}

public class ConnStrings
{
    public Dictionary<string, string> ConnectionStrings { get; set; } = new Dictionary<string, string>() {{"OracleKeyManagement","The Connection String"}};
};

public class Redis 
{
    public string Host { get; set; } = "localhost";
    public string Port { get; set; } = "The Redis Port";
}

1 个答案:

答案 0 :(得分:0)

如何简单地使用词典类?像:

public class ModuleConfig
{
    IpAddress { get; set; } = "The IP",
    Port { get; set; } = "The Module Port",
    Buffer { get; set; } = "1024",
    public Dictionary<string, string> ConnectionStrings { get; set; }
    public Redis RedisConfig { get; set } 
}
相关问题