Sharing application configurations in a solution

I have a solution that has several output projects (website, admin tool and SOAP API layer).

Each of them has common projects in the solution (service level, data level, etc.). In one of these common projects, I want to keep the configuration layer.

Right now, we have three separate appsettings configuration files for each output project -

  • development.AppSettings.config
  • testing.AppSettings.config
  • production.AppSettings.config

Thus, there are nine configuration files. Only one is used in each project, since they refer to the use of the configSource attribute in the web.config node settings.

Anyhoo, it gets pain anytime we want to add / remove values ​​from our configuration files, because that means we have to change all nine files for this. And here is what I would like to do:

In a common project, we have three configuration files, as described above. They must be copied to the output directory so that each project has a copy of them. This will be the "basic" configuration.

Then in each project I would like to have three files again, but they do not have to contain the same values ​​as the base configurations. If they did, then the base configuration value would be overridden by the value in the configuration of the output project. The configuration inheritance form, I suppose.

- . .

, , ? , , , , , ?

, production.appsettings.config, , .

(//), , ?

+3
3

03:30 , .

, appSettings :

<add key="MyKey1" value="MyValue1" />
<add key="MyKey2" value="MyValue2" />
<!-- And so on... -->
<add key="MyKey5" value="MyValue5" />

appSettings:

<!-- This is used to identify which config to use. -->
<add key="Config" value="Development" />

<!-- Different value to the one in the base -->
<add key="MyKey2" value="NewValue2" />

<!-- This key does not exist in the base config -->
<add key="MyKey6" value="MyValue6" />

Application_Start GetConfigs():

ConfigHelper.GetConfig(HostingEnvironment.MapPath( "~//BaseConfig" ));

GetConfigs:

public static void GetConfigs()
{
  if (configMode == null)
  {
    configMode = ConfigurationManager.AppSettings.Get("Config").ToLowerInvariant();
  }

  //Now load the app settings file and retrieve all the config values.
  var config = XElement.Load(@"{0}\AppSettings.{1}.config".FormatWith(directory, configMode))
    .Elements("add")
    .Select(x => new { Key = x.Attribute("key").Value, Value = x.Attribute("value").Value })
    //If the current application instance does not contain this key in the config, then add it.
    //This way, we create a form of configuration inheritance.
    .Where(x => ConfigurationManager.AppSettings.Get(x.Key) == null);

  foreach (var configSetting in config)
  {
      ConfigurationManager.AppSettings.Set(configSetting.Key, configSetting.Value);
  }
}

:

<add key="Config" value="Development" />
<add key="MyKey1" value="MyValue1" />
<add key="MyKey2" value="NewValue2" />
<!-- And so on... -->
<add key="MyKey5" value="MyValue5" />
<add key="MyKey6" value="MyValue6" />

Simples!

+1
+1

Source: https://habr.com/ru/post/1733081/


All Articles