Load a custom appsettings file based on prior appsettings files

Viewed 29

I am trying to have a number of appsettings files built for integrated environments that this application talks too. Based on what is in appsettings.<Environment>.json, I would like to load another appsettings file.

I'm trying similar to the following in program.cs:

IConfiguration tempConfiguration = config.Build();
string[] items = tempConfiguration.GetValue<string[]>("Application:EnvironmentPointers");
foreach (string environment in items)
{ 
    config.AddJsonFile($"integratedApplication.{environment}.json", optional: false, reloadOnChange: true);
}

This is throwing a null reference exception at the GetValue<T>() section.

The appsettings.<Environment>.json looks similar to the following:

{
  "Application": {
    "EnvironmentPointers": [ "Development", "Staging" ]
  }
}

Any ideas how this could be achieved?

1 Answers

Consider changing to the following approach instead

//...

string[] items = tempConfiguration.GetSection("Application:EnvironmentPointers").Get<string[]>();

//...
Related