简体   繁体   English

.NET控制台应用程序未读取config.json

[英].NET console app not reading config.json

While building a .net console app which uses the new ConfigurationBuilder implementation for what used to be appSettings , I've run into a problem. 在构建.net控制台应用程序时,该应用程序将新的ConfigurationBuilder实现用于以前的appSettings ,但遇到了一个问题。

I have the following code: 我有以下代码:

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)
    {
        ...
    }
}

The config.json file is in the same directory and looks like this: config.json文件位于同一目录中,如下所示:

{
  "Azure": {
    "Storage": {
      "ConnectionString": "...",
      "ContainerName": "..."
    }
  },
  "Data": {
    "DefaultConnection": {
      "ConnectionString": "..."
    }
  },
  "Logging": {
    "RecordProgress": "..."
  }
}

But the config object contains no keys. 但是config对象不包含任何键。

I read somewhere that if the file path passed to AddJsonFile can't be found then it throws a FileNotFoundException but in my code that exception is never thrown. 我在某处读到,如果找不到传递给AddJsonFile的文件路径,那么它将引发FileNotFoundException但是在我的代码中,永远不会引发异常。

So presuming the config.json file can be found, why are the settings not being loaded? 因此,假设可以找到config.json文件,为什么未加载设置?

My original answer was off the mark. 我原来的答案是不可能的。 Here's an updated version. 这是更新版本。 This is based on the recently released RC2. 这基于最近发布的RC2。

The current runtime will throw FileNotFoundException if the config file isn't found. 如果找不到配置文件,当前运行时将抛出FileNotFoundException The AddJsonFile() extension method takes an optional parameter called optional that, if true, will cause the method not to throw. AddJsonFile()扩展方法带有一个称为optional的可选参数,如果为true,它将导致该方法不抛出。

I added config.json and it doesn't get copied to the bin directory, so I had to specify the location using the SetBasePath() extension method. 我添加了config.json ,它不会被复制到bin目录中,因此我不得不使用SetBasePath()扩展方法来指定位置。 This is what the Web project templates do in Startup, using IHostingEnvironment.ContentRootPath . 这是Web项目模板在启动时使用IHostingEnvironment.ContentRootPath In console apps, you can use Directory.GetCurrentDirectory() . 在控制台应用程序中,可以使用Directory.GetCurrentDirectory()

var builder = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("config.json");

var config = builder.Build();

Finally, the config["Key"] indexer didn't work for me. 最后, config["Key"]索引器对我不起作用。 Instead, I had to use the GetSection() extension method. 相反,我不得不使用GetSection()扩展方法。 So your example config file above might be accessed with: 因此,您可以使用以下示例访问上面的示例配置文件:

// var result = config["Logging"];
var section = config.GetSection("Logging");
var result = section["RecordProgress"];

I left the old answer for the time being. 我暂时离开了旧答案。

Old answer: I found a possible solution here: https://github.com/aspnet/Mvc/issues/4481 . 旧答案:我在这里找到了可能的解决方案: https : //github.com/aspnet/Mvc/issues/4481

Quoting from the issue. 从问题报价。

Thanks for the repro project. 感谢您的repro项目。 Looks like you need to update the project.json file to have the "content" node and have Config.json specified here. 看起来您需要更新project.json文件以具有“ content”节点,并在此处指定Config.json。

Example: https://github.com/aspnet/MusicStore/blob/dev/src/MusicStore/project.json#L22 示例: https//github.com/aspnet/MusicStore/blob/dev/src/MusicStore/project.json#L22

It appears that a new content element may be required in your project.json . 看来您的project.json可能需要一个新的content元素。

... "content": [ "Areas", "Views", "wwwroot", "config.json", "web.config" ], ...

I encountered the same problem. 我遇到了同样的问题。

config.json was in the correct directory and it was still not found. config.json在正确的目录中,但仍未找到。

The solution: config.json contained a typo that broke the json syntax. 解决方案:config.json包含拼写错误,该拼写错误破坏了json语法。 After fixing the json syntax/format in config.json everything worked. 在config.json中修复json语法/格式后,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM