代码之家  ›  专栏  ›  技术社区  ›  Dan Atkinson

跨解决方案共享应用程序配置

  •  3
  • Dan Atkinson  · 技术社区  · 14 年前

    我有一个有多个输出项目的解决方案(一个网站、管理工具和一个soap api层)。

    它们各自共享解决方案中的公共项目(服务层、数据层等)。在这些常见的项目中,我希望存储一个配置层。

    现在,每个输出项目都有三个单独的appsettings配置文件-

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

    总共有9个配置文件。每个项目中只使用一个,因为它们是通过使用 configSource web.config appsettings节点中的属性。

    不管怎样,每当我们想从配置文件中添加/删除值时,都会很痛苦,因为这意味着我们必须更改所有 要执行此操作的文件。我想做的是:

    在公共项目中,我们有三个如上所述的配置文件。将这些设置为复制到输出目录,以便每个项目都有它们的副本。这就是“基本”配置。

    然后在每个项目中,我希望再次拥有三个文件,但它们不必包含与基本配置相同的值。 但是,如果它们这样做了,那么基本配置值将被输出项目配置中的值覆盖。我想这是一种配置继承。

    在应用程序启动时,我希望能够获得这两个配置文件-基本配置和项目配置文件。然后相应地设置应用程序设置。

    不过,我想知道的是,什么是确定要使用哪个文件的好方法?另外,我想知道这是否是一种跨大型解决方案共享应用程序值的好方法,以及是否有另一种更有效的方法?

    如果我处于开发模式,则不需要production.appsettings.config;如果我处于生产模式,则相反。

    在我离开并获取配置之前,有没有一个简单的方法获得我所处的模式(开发/测试/生产)?

    3 回复  |  直到 10 年前
        1
  •  1
  •   Burt    14 年前

    您可以拥有一组文件(3个配置)并在需要的任何项目中链接/共享它们。

    http://www.devx.com/vb2themax/Tip/18855

    希望这有帮助。

        2
  •  1
  •   Brownman98    14 年前

    你可以用 ConfigurationManager.OpenExeConfiguration 静态方法。这将允许您使用任意数量的配置文件。

    您也可以尝试创建一个自定义类来存储所有设置。然后可以序列化对象以将其另存为文件。您可以扩展基本自定义配置类以适合所有其他项目。

        3
  •  1
  •   Dan Atkinson    11 年前

    经过深思熟虑,在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" />
    

    在我的申请中,我有一个电话 GetConfigs() :

    confighelper.getconfig(hostingEnvironment.mapPath(“~/bin/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" />
    

    简单!