我正在尝试为要从 appsettings.json 加载的配置构建类。
这是我遇到问题的 appsettings.json 部分。我无法弄清楚如何为 ConnectionStrings 构建对象。下面的示例不允许多个连接字符串,我们不想根据 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";
}
简单地使用字典类怎么样?喜欢:
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 }
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句