【发布时间】:2016-08-21 13:57:23
【问题描述】:
在构建一个使用新的 ConfigurationBuilder 实现的 .net 控制台应用程序时,我遇到了一个问题。
我有以下代码:
public static void Main(string[] args)
{
try
{
var builder = new ConfigurationBuilder().AddJsonFile("config.json");
var config = builder.Build();
if (config["Azure"] != null)
{
;
}
}
catch (System.IO.FileNotFoundException exception)
{
...
}
}
config.json 文件位于同一目录中,如下所示:
{
"Azure": {
"Storage": {
"ConnectionString": "...",
"ContainerName": "..."
}
},
"Data": {
"DefaultConnection": {
"ConnectionString": "..."
}
},
"Logging": {
"RecordProgress": "..."
}
}
但是config 对象不包含键。
我在某处读到,如果无法找到传递给 AddJsonFile 的文件路径,那么它会抛出 FileNotFoundException,但在我的代码中永远不会抛出异常。
那么假设config.json文件可以找到,为什么没有加载设置?
【问题讨论】:
标签: c# .net json configuration configurationmanager