0

I am following the example shown here: https://stackoverflow.com/a/65111169

However, I can't seem to get it to work because I get an error message: IConfiguration does not contain a definition for Get.... I have imported Microsoft.Extensions.Configuration and Microsoft.Extensions.Configuration.Jason, both version 3.1.32. Why doesn't it work? I also tried nesting my json file inside a Settings property so that I could try this: var myFirstClass = config.GetSection("Settings").GetValue<MyFirstClass>("Settings","myFirstClass"); When I use this GetValue syntax I have no errors, but my variable is null.

    static void Main(string[] args)
    {
        var builder = new ConfigurationBuilder()
           .SetBasePath(Directory.GetCurrentDirectory())
           .AddJsonFile("appsettings.json", optional: false);

        IConfiguration config = builder.Build();

            var myFirstClass = config.GetSection("myFirstClass").Get<MyFirstClass>();
    }

My appsettings.json

{
    "MyFirstClass": {
      "Option1": "some string value",
      "Option2": 42
    },
    "MySecondClass": {
      "SettingOne": "some string value",
      "SettingTwo": 42
    }
}
mo_maat
  • 2,110
  • 12
  • 44
  • 72

0 Answers0