1

I have the following code.

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

string beep = config.GetSection("beep").Value;

My config.json looks like this.

{ "beep": "bopp" }

When I hit a breakpoint I can see that there's one provider but the data in it is of zero length. I've tried different approaches as config["beep"] etc. but sems to fail to get the value in. It's null all the time. I'm trying to follow the docs but must be missing something.

3
  • 1
    Doesn't the json have to look something like this? {"Name":{"beep":"boop"}}. Then you can say config.GetSection("Name"). Commented Jan 18, 2019 at 22:50
  • do you have control of how the config.json looks? Commented Jan 18, 2019 at 22:54
  • @DJBurb He should, you write the appsettings.json however you want. Konrad, are you doing that code directly in main or are you leveraging any other aspects of core? Such as IServiceCollection? Commented Jan 18, 2019 at 22:55

2 Answers 2

4

Make sure the json file is set to copy to directory as discussed in this blog. Otherwise when you build or debug the configuration file won’t be included. Make sure you change it in the properties.

The reason you are not able to access your configuration, is because the IConfigurationRoot does not reference the dependency for ConfigurationBuilder. To ensure that your configuration content will load, would be to do something along these lines:

public static class ConfigurationProvider
{
     public static IConfiguration BuildConfiguration => new ConfigurationBuilder()
          .SetBasePath(Directory.GetCurrentDirectory())
          .AddJsonFile("appsettings.json", true, true)
          .Build();
}

The above will build our configuration, now we should use the configuration.

public static class ServiceProvider
{
     public static IServiceProvider BuildServiceProvider(IServiceCollection services) => services
          .BuildDependencies()
          .BuildServiceProvider();
}

Once we have defined our provider, we can do the following so we can pass our IConfiguration around the application to access the objects.

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

var serviceProvider = ServiceProvider.BuildServiceProvider(serviceCollection);

Then inside of another class, you would have the following:

public class SampleContext : ISampleRepository
{
     private readonly string dbConection;

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

     ...
 }

Then our appsettings.json would look like this:

{
  "ConnectionStrings": {
    "dbConnection": "..."
  }
}

The above is the correct format for a json object. If you have a nested object along these lines:

{
     "Sample" : {
          "Api" : {
               "SampleGet" : "..."
          }
     }
}

Then your C# would be:

configuration.GetSection("Sample:Api")["SampleGet"];

The above is based on the assumption your configuration and usage are not in the same sequential area, ie directly in your main. Also you should use appsettings.json since that is the default, less extra wiring if I remember correctly. Your json also needs to be correctly formatted.

But that will definately work, if you need more help let me know and I can send you some sample console core applications to demonstrate usage.

Sign up to request clarification or add additional context in comments.

Comments

1

I feel like you are just missing the name for the object.

Try adding a name for the object in the config.json like so:

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

Then you can do string beep =config.GetSection("beep").Value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.