将配置文件的部分加载到 net 5.0 中的字典中
我必须研究我不知道的未开发的Net 5.0的应用程序。
我想在配置文件中添加一个部分,然后将其加载到字典中。
这是配置文件中的一节:
{
"MyApp": {
"ServiceExceptions": [
{ "Key1": "Value1" },
{ "Key2": "Value2" }
]
}
}
此代码加载配置:
public static IConfiguration GetConfiguration(string name)
{
IConfiguration oConfiguration = null;
string basePath = Path.GetDirectoryName(typeof(SharedHelper).Assembly.Location);
string configPath = Path.Combine(basePath, "Config");
string fileConfig = string.Format("{0}.{1}.json", name, Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));
oConfiguration = new ConfigurationBuilder()
.SetBasePath(configPath)
.AddJsonFile(fileConfig, optional: false, reloadOnChange: true)
.Build();
return oConfiguration;
}
这是加载以绑定字典的代码,但是myExceptions为null:
_Configuration = GetConfiguration("BD.Authentication.DWA");
Dictionary<string, string> myExceptions = null;
_Configuration.GetSection("MyApp:ServiceExceptions").Bind(myExceptions);
Debug.WriteLine(myExceptions.Count); // throws exception null reference
在某个地方加载了数据,但是我不知道如何拉出
I have to work on an application in net 5.0 not developed by me, that I don't know.
I would like to add a section in the config file and load it into a dictionary.
this is the section in config file:
{
"MyApp": {
"ServiceExceptions": [
{ "Key1": "Value1" },
{ "Key2": "Value2" }
]
}
}
this code load the configuration:
public static IConfiguration GetConfiguration(string name)
{
IConfiguration oConfiguration = null;
string basePath = Path.GetDirectoryName(typeof(SharedHelper).Assembly.Location);
string configPath = Path.Combine(basePath, "Config");
string fileConfig = string.Format("{0}.{1}.json", name, Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));
oConfiguration = new ConfigurationBuilder()
.SetBasePath(configPath)
.AddJsonFile(fileConfig, optional: false, reloadOnChange: true)
.Build();
return oConfiguration;
}
this is the code to load to bind the dictionary, but myExceptions is null:
_Configuration = GetConfiguration("BD.Authentication.DWA");
Dictionary<string, string> myExceptions = null;
_Configuration.GetSection("MyApp:ServiceExceptions").Bind(myExceptions);
Debug.WriteLine(myExceptions.Count); // throws exception null reference
somewhere are loaded the data, but i dont know how to pull out
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需将配置文件移入:
并检验值:
Just move the config file in:
and retrive the values: