C# 无法从.NET Core 2控制台应用程序中的config.json读取数据

C# 无法从.NET Core 2控制台应用程序中的config.json读取数据,c#,configuration,.net-core,C#,Configuration,.net Core,我有以下代码 IConfigurationRoot config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("config.json", true, true) .Build(); string beep = config.GetSection("beep").Value; 我的config.json如下所示 { "beep": "bopp" }

我有以下代码

IConfigurationRoot config = new ConfigurationBuilder()
  .SetBasePath(Directory.GetCurrentDirectory())
  .AddJsonFile("config.json", true, true)
  .Build();

string beep = config.GetSection("beep").Value;
我的config.json如下所示

{ "beep": "bopp" }

当我点击断点时,我可以看到有一个提供程序,但其中的数据长度为零。我尝试了不同的方法,如config[beep]等,但sems无法获取值。它一直是空的。我正试着跟随,但一定是遗漏了什么。

我觉得您只是遗漏了对象的名称

尝试在config.json中添加对象的名称,如下所示:

{"beep":{"beep":"bopp"}}

然后可以执行string beep=config.GetSectionbeep.Value

我觉得您只是缺少对象的名称

尝试在config.json中添加对象的名称,如下所示:

{"beep":{"beep":"bopp"}}

然后可以执行string beep=config.GetSectionbeep.Value

确保json文件设置为复制到目录,如中所述。否则,在生成或调试时,将不包括配置文件。请确保在属性中对其进行更改

无法访问配置的原因是IConfigurationRoot未引用ConfigurationBuilder的依赖项。要确保您的配置内容将被加载,请按照以下方式执行操作:

public static class ConfigurationProvider
{
     public static IConfiguration BuildConfiguration => new ConfigurationBuilder()
          .SetBasePath(Directory.GetCurrentDirectory())
          .AddJsonFile("appsettings.json", true, true)
          .Build();
}
{
     "Sample" : {
          "Api" : {
               "SampleGet" : "..."
          }
     }
}
上面将构建我们的配置,现在我们应该使用配置

public static class ServiceProvider
{
     public static IServiceProvider BuildServiceProvider(IServiceCollection services) => services
          .BuildDependencies()
          .BuildServiceProvider();
}
定义了提供者之后,我们可以执行以下操作,以便在应用程序中传递IConfiguration来访问对象

var serviceCollection = new ServiceCollection()
     .AddSingleton(configuration => ConfigurationProvider.BuildConfiguration());

var serviceProvider = ServiceProvider.BuildServiceProvider(serviceCollection);
然后在另一个类中,您将拥有以下内容:

public class SampleContext : ISampleRepository
{
     private readonly string dbConection;

     public SampleContext(IConfiguration configuration) => configuration.GetConnectionString("dbConnection");

     ...
 }
然后我们的appsettings.json将如下所示:

{
  "ConnectionStrings": {
    "dbConnection": "..."
  }
}
以上是json对象的正确格式。如果沿以下直线有嵌套对象:

public static class ConfigurationProvider
{
     public static IConfiguration BuildConfiguration => new ConfigurationBuilder()
          .SetBasePath(Directory.GetCurrentDirectory())
          .AddJsonFile("appsettings.json", true, true)
          .Build();
}
{
     "Sample" : {
          "Api" : {
               "SampleGet" : "..."
          }
     }
}
那么你的C应该是:

configuration.GetSection("Sample:Api")["SampleGet"];
上述假设是基于您的配置和使用不在同一顺序区域,即直接在您的主目录中。另外,您应该使用appsettings.json,因为这是默认设置,如果我没记错的话,可以减少额外的连接。您的json还需要正确格式化


但是,如果您需要更多帮助,请告诉我,我可以向您发送一些示例控制台核心应用程序来演示其用法。

确保json文件设置为“复制到目录”,如中所述。否则,在生成或调试时,将不包括配置文件。请确保在属性中对其进行更改

无法访问配置的原因是IConfigurationRoot未引用ConfigurationBuilder的依赖项。要确保您的配置内容将被加载,请按照以下方式执行操作:

public static class ConfigurationProvider
{
     public static IConfiguration BuildConfiguration => new ConfigurationBuilder()
          .SetBasePath(Directory.GetCurrentDirectory())
          .AddJsonFile("appsettings.json", true, true)
          .Build();
}
{
     "Sample" : {
          "Api" : {
               "SampleGet" : "..."
          }
     }
}
上面将构建我们的配置,现在我们应该使用配置

public static class ServiceProvider
{
     public static IServiceProvider BuildServiceProvider(IServiceCollection services) => services
          .BuildDependencies()
          .BuildServiceProvider();
}
定义了提供者之后,我们可以执行以下操作,以便在应用程序中传递IConfiguration来访问对象

var serviceCollection = new ServiceCollection()
     .AddSingleton(configuration => ConfigurationProvider.BuildConfiguration());

var serviceProvider = ServiceProvider.BuildServiceProvider(serviceCollection);
然后在另一个类中,您将拥有以下内容:

public class SampleContext : ISampleRepository
{
     private readonly string dbConection;

     public SampleContext(IConfiguration configuration) => configuration.GetConnectionString("dbConnection");

     ...
 }
然后我们的appsettings.json将如下所示:

{
  "ConnectionStrings": {
    "dbConnection": "..."
  }
}
以上是json对象的正确格式。如果沿以下直线有嵌套对象:

public static class ConfigurationProvider
{
     public static IConfiguration BuildConfiguration => new ConfigurationBuilder()
          .SetBasePath(Directory.GetCurrentDirectory())
          .AddJsonFile("appsettings.json", true, true)
          .Build();
}
{
     "Sample" : {
          "Api" : {
               "SampleGet" : "..."
          }
     }
}
那么你的C应该是:

configuration.GetSection("Sample:Api")["SampleGet"];
上述假设是基于您的配置和使用不在同一顺序区域,即直接在您的主目录中。另外,您应该使用appsettings.json,因为这是默认设置,如果我没记错的话,可以减少额外的连接。您的json还需要正确格式化


但是,如果您需要更多帮助,请告诉我,我可以向您发送一些控制台核心应用程序示例,以演示其使用情况。

json不是必须看起来像这样吗?{Name:{beep:boop}}。然后你可以说config.GetSectionName.you可以控制config.json的外观吗?@DJBurb他应该,你可以随心所欲地编写appsettings.json。Konrad,你是直接在main中编写代码,还是在利用core的其他方面?比如IServiceCollection?json不是必须看起来像这样吗?{Name:{beep:boop}}。然后你可以说config.GetSectionName.you可以控制config.json的外观吗?@DJBurb他应该,你可以随心所欲地编写appsettings.json。Konrad,你是直接在main中编写代码,还是在利用core的其他方面?比如IServiceCollection?