简体   繁体   中英

Load a custom appsettings file based on prior appsettings files

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?

Consider changing to the following approach instead

//...

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

//...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM