Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't read appsettings.json without DI due to missing Configuration Builder

I'm trying to read from appsettings.json and I don't want to use DI. I simply wish to access the file. I'm following simple configuration as suggested by MS.

using System.IO;
using Microsoft.Extensions.Configuration;

class Program
{
  public static IConfigurationRoot Configuration { get; set; }

  static void Main(string[] args)
  {
    IConfigurationBuilder builder = new ConfigurationBuilder()
      .SetBasePath(Directory.GetCurrentDirectory())
      .AddJsonFile("appsettings.json");

    Configuration = builder.Build();
  }
}

The classes get from red highlight to green when I specify the reference to Microsoft.Extensions.Configuration except for one - the Configuration Builder instance creation. It produces the following error.

Error CS0246 The type or namespace name 'ConfigurationBuilder' could not be found (are you missing a using directive or an assembly reference?)

All the examples I've found are set up the same way. Even the docs for Configuration Builder say that it's supopsed to be there. I'm at a loss on how to proceed.

I'm using .NET Core 2.0 if it's of any significance.


1 Answers

AddJsonFile method is in the Microsoft.Extensions.Configuration.Json package and in the Microsoft.Extensions.Configuration namespace.

So Microsoft.Extensions.Configuration.Json package need to be added to project.Once all the packages are restored successfully it works perfectly. If you have already added them another error might be blocking the build before that point gets compiled.

like image 167
nandithakw Avatar answered Oct 28 '25 03:10

nandithakw