11

I want to load multiple config files from a folder in a loop in .Net core middle ware.

I know we can load one or multiple config files by naming them like appSettings.json as mentioned here

But in my case, if I have multiple config folders, and each folder has multiple config files to be loaded initially. And If I start naming each file in each folder to load, it will be lot of lines and messier. I am looking to load all of the config folders in loop.

Please ask if need more info. Thanks

Syed
  • 308
  • 1
  • 4
  • 12
  • How are you loading configuration files currently? Are you using `WebHost.CreateDefaultBuilder` defaults or using your own code? Also, should the files be loaded in a particular order? – Kirk Larkin May 23 '18 at 11:30
  • Yes I am using WebHost.CreateDefaultBuilder defaults. And I am not loading everything so far, but that's the goal. No order is not particular – Syed May 23 '18 at 11:43

3 Answers3

13

You can achieve this using something like Directory.EnumerateFiles and ConfigureAppConfiguration. Here's an example of what this might look like:

WebHost.CreateDefaultBuilder(args)
    .UseStartup<Startup>()
    .ConfigureAppConfiguration(configurationBuilder =>
    {
        foreach (var jsonFilename in Directory.EnumerateFiles("/path/to/jsons", "*.json", SearchOption.AllDirectories))
            configurationBuilder.AddJsonFile(jsonFilename);
    })
    .Build();

The call to ConfigureAppConfiguration allows for adding additional providers to the configuration system. Here, we're just adding all *.json files found within the /path/to/jsons directory (and children) as additional configuration sources.

Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203
  • 1
    Once you've done this, how do you extract the values? Configuration.GetSection( "Type" ).Get(); only gets the value from the original appconfig.json file. – Markus Jun 03 '19 at 13:12
  • 1
    @Markus Have a look at the docs [here](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.2#getvalue) and maybe [here](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-2.2). The values in the additional files should take precedence, so there might be something else amiss in your scenario. Feel free to open a new question and link to it here. – Kirk Larkin Jun 03 '19 at 13:15
  • 1
    it actually did use the second file's values. I guess the problem is that the section names are the same in both files. But I need them to be the same, and their contents merged. I found [this](https://stackoverflow.com/questions/53985133/how-to-merge-multiple-arrays-from-asp-net-core-configuration-files), which just tries to sidestep the issue. – Markus Jun 03 '19 at 15:42
  • 1
    @Markus Yeah, you can't merge the values. Later providers override values for earlier providers. – Kirk Larkin Jun 03 '19 at 15:49
  • For me, one question remains : when you load several Json files using the `AddJsonFile` method, how does it deal with duplicate keys ? Will it throw an error ? Will it replace the key value with the one from the last added Json file ? – Ishikawa Dec 06 '22 at 09:53
  • 1
    @Ishikawa Those added later _override_ those loaded before. – Kirk Larkin Dec 07 '22 at 09:02
5

Well, i had to figure out it with an extension method to the ConfigurationBuilder class. Hope it helps to anybody in need.

 public static IConfigurationBuilder AddMultipleJsonFiles(this IConfigurationBuilder configurationBuilder, string path, string prefix)
    {
        string[] files = System.IO.Directory.GetFiles(path, prefix + "-*.json");

        foreach (var item in files)
        {
            configurationBuilder.AddJsonFile(item);
        }
        return configurationBuilder;
    }

And also;

 var builder = new ConfigurationBuilder().AddMultipleJsonFiles(Directory.GetCurrentDirectory()+ "/Configurations/", "service");
            _configuration = builder.Build();
MonkeyDLuffy
  • 556
  • 7
  • 17
4

I am developing an ASP.NET Core Web App MVC & API with .NET 6. There is no startup.cs but program.cs. Therefore, I tried to add builder.Configuration.AddJsonFile("myJsonFile.json"), and it works for me.

In program.cs,

var builder = WebApplication.CreateBuilder(args);


// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Configuration.AddJsonFile("myJsonFile.json");

var app = builder.Build();
...
Sinji Yang
  • 89
  • 2