1

As far as what I understand:

appSettings is a dictionary of key value pair. But I can't find how to add a list of item as the value. Also, I would like to add my own object as the value if possible.

Is there a way to add a List of MyConfigObject (List<MyConfigObject>)into a config file? If yes, how? Should I use another section or should I use another type of config file (json, yaml) to have the simplest way to read/write settings?

Eric Ouellet
  • 10,996
  • 11
  • 84
  • 119

2 Answers2

1

Yes, you can, Like this:

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "MyConfigObject": [
    {
      "Prop1": "1",
      "Prop2": "2"
    },
    {
      "Prop1": "1",
      "Prop2": "2"
    }
  ]
}

MyConfigObject Class

public class MyConfigObject
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
}

In Startup, register the Configuration with the type MyConfigObject. Like this:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
        services.Configure<List<MyConfigObject>>(Configuration.GetSection("MyConfigObject"));
    }

Now you can use it in any service or controller, like this:

public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly List<MyConfigObject> myConfig;
        public HomeController(IOptions<List<MyConfigObject>> myConfig, ILogger<HomeController> logger)
        {
            _logger = logger;
            this.myConfig = myConfig.Value;
        }

        public IActionResult Index()
        {
            return View();
        }
}
Ibrahim Hamaty
  • 540
  • 2
  • 18
  • Thank you very much!!! Your answer is just awesome. I'm currently developping a windows service and there is no "services" by default. I'm looking to see what's the best way to implement your suggestion with the fewer dependency on the "services". I would like to depends only the JSON reader and property matcher. But I will see if there is already a "services" OR if I should add one into a windows service OR if I can just read the config without going with IOC(services). I will get back to you when I will find some answers... – Eric Ouellet Jun 06 '23 at 15:17
0

For the those who wonder how to get a JSON config section into an object without injection into a services:

var appFolder = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);

    Configuration = new ConfigurationBuilder()
        .AddJsonFile(Path.Combine(appFolder, "appsettings.json"))
        .AddEnvironmentVariables()
        .Build();

    IConfigurationSection sectionMyConfigObject = Configuration.GetSection(nameof(MyConfigObject));
    var myConfigObject = sectionMyConfigObject.Get<List<MyConfigObject>>();

For file appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "MyConfigObject": [
    {
      "Prop1": "1",
      "Prop2": "2"
    },
    {
      "Prop1": "1",
      "Prop2": "2"
    }
  ]
}
Eric Ouellet
  • 10,996
  • 11
  • 84
  • 119