1

I have a .Net 6 console app.

I have installed (Latest versions)

Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.Json

I manually create an appsettings.json file in the root folder.

In my program.cs i add

var configuration = new ConfigurationBuilder()
     .AddJsonFile($"appsettings.json");

var config = configuration.Build();
var customString = config.GetSection("Custom:Customisation");

The JSON file contents are

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "Custom": {
    "Customisation": {
      "FieldOne": "ThisIsValueOne",
      "FieldTwo": "ValueTwo"
    }
  },

  "Something": {
    "SomthingElsoe": {
      "MoreSetting": "1"
    }

  }
}
  1. Its finds the section but i cant see any of the values for FieldOne etc, what change do i need?
  2. Could i convert this to a C# class so i have strongly typed properties with values?

Regarding question 2 i did research on this and i noticed i needed builder.Services but this isnt available in the Console app?

  • Please avoid asking multiple questions in one post. – Zohar Peled Jun 20 '23 at 10:34
  • 1
    The code doesn't try to read anything. `GetSection` returns an `IConfigurationSection` , not individual strings in it. Try `var section = config.GetSection("Custom:Customisation");` then `section["FieldOne"]` or `section.GetValue("FieldOne")` to get the actual value. – Panagiotis Kanavos Jun 20 '23 at 10:35
  • The [Configuration docs](https://learn.microsoft.com/en-us/dotnet/core/extensions/configuration) show how to load settings as objects with eg `section.Get()` or using the [Options middleware](https://learn.microsoft.com/en-us/dotnet/core/extensions/options). GetSection etc. are show in the [ASP.NET Core Configuration doc](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-7.0#getvalue) which is ..... a bit too long – Panagiotis Kanavos Jun 20 '23 at 10:43
  • @PanagiotisKanavos Perfect that did the trick. I cant mark this as an answer though (doesnt give me the option), i will copy and paste this question and ask question 2 – KeithViking Jun 20 '23 at 10:43
  • Check this tutorial if you want to use DI, Configuration, etc in Console app - https://youtu.be/GAOCe-2nXqc – Dušan Jun 20 '23 at 10:56

1 Answers1

2

You Can use this code for solve your problem that First Read Data and Convert To class

You must install packages below

Microsoft.Extensions.Configuration.FileExtensions 
Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.Json
var builder = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json", optional: false);

IConfiguration config = builder.Build();
var myFirstClass = config.GetSection("Custom:Customisation").Get<Customisation>();

Class

 public class Customisation
    {
        public string FieldOne { get; set; }
        public string FieldTwo { get; set; }
    }
abolfazl sadeghi
  • 2,277
  • 2
  • 12
  • 20