【问题标题】:Different application settings depending on configuration mode不同的应用程序设置取决于配置模式
【发布时间】:2011-03-06 15:23:14
【问题描述】:

是否有人知道我可以在 .Net 应用程序中设置应用程序(或用户)级别设置的方法,这些设置取决于应用程序当前的开发模式? IE:调试/发布

更具体地说,我的应用程序设置中有一个指向我的 web 服务的 url 引用。在发布模式期间,我希望这些设置在调试模式期间指向http://myWebservice.MyURL.com,我希望这些设置为http://myDebuggableWebService.MyURL.com

有什么想法吗?

【问题讨论】:

    标签: c# .net vb.net settings appsettings


    【解决方案1】:

    这有点晚了,但我偶然发现了一种为app.config 文件实现web.transform 方法的好方法。 (即它使用命名空间http://schemas.microsoft.com/XML-Document-Transform

    我认为它“不错”,因为它是一种纯 xml 方法,不需要 3rd 方软件。

    • 根据您的各种构建配置,父/默认 App.config 文件源自。
    • 这些后代只覆盖他们需要的。

    在我看来,这比维护x 的配置文件数量要复杂和强大得多,这些配置文件会被完整复制,例如在其他答案中。

    这里发布了一个演练: http://mitasoft.wordpress.com/2011/09/28/multipleappconfig/


    妈妈,看,我的 IDE 中没有明确的构建后事件!

    【讨论】:

    • 工作,谢谢!如果您使用的是 VS 2017 并且出现找不到 Web*.targets 的错误,请查看此答案stackoverflow.com/a/45354395/2964949
    • 对于 VS 2017,v10.0 必须在 <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v15.0\Web\Microsoft.Web.Publishing.targets" /> 中替换为 v15.0
    • 对任何 Visual Studio 版本使用 Project=”$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.targets”(来自链接中的 cmets)。
    • 现在有一个 VS 插件:Configuration Transform
    【解决方案2】:

    我知道这是几年前提出的问题,但以防万一有人正在寻找我使用的简单有效的解决方案。

    1. 转到项目属性的“设置”选项卡(您将看到您的 Web 服务 URL 或此处已列出的任何其他设置)。

    2. 点击设置页面上的“查看代码”按钮。

    3. 在构造函数中输入这个。

      this.SettingsLoaded += Settings_SettingsLoaded;
      
    4. 在构造函数下添加如下函数:

      void Settings_SettingsLoaded(object sender, System.Configuration.SettingsLoadedEventArgs e)
      {
          #if(DEBUG)
          this["YOUR_SETTING_NAME"] = VALUE_FOR_DEBUG_CONFIGURATION;
          #else
          this["YOUR_SETTING_NAME"] = VALUE_FOR_RELEASE_CONFIGURATION;
          #endif
      }
      

    现在,无论何时运行项目,它都只会编译与当前构建配置匹配的行。

    【讨论】:

    • 您在哪里/如何指定每个配置的更改?例如,如果我创建一个名为“QARelease”的配置,我将如何检查这是当前的配置?
    • 不确定我是否理解正确,但是有 #if(DEBUG) 预处理器指令用于区分 DEBUG 和 RELEASE 配置。此外,您可以为每个配置定义自己的编译符号并在 #if 中使用它们。
    • 谢谢。我不确定您的 DEBUG 来自哪里。
    【解决方案3】:

    据我所知,没有内置的方法可以做到这一点。在我们的项目中,我们维护了 4 个不同的设置文件,并通过在构建的预构建步骤中将每个设置复制到活动文件中来在它们之间切换。

    copy "$(ProjectDir)properties\settings.settings.$(ConfigurationName).xml" "$(ProjectDir)properties\settings.settings"
    copy "$(ProjectDir)properties\settings.designer.$(ConfigurationName).cs" "$(ProjectDir)properties\settings.Designer.cs"
    

    这对我们来说已经好几年了。只需更改目标,整个配置文件也会切换。

    编辑:文件被命名为例如settings.settings.Debug.xmlsettings.settings.Release.xml 等..

    Scott Hanselman 描述了一种稍微“更智能”的方法,唯一的区别是我们没有检查文件是否已更改: http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx

    【讨论】:

      【解决方案4】:

      如果您想将所有内容保存在一个配置文件中,您可以在 app.settings 中引入自定义配置部分,以存储调试和发布模式的属性。

      您可以将对象持久保存在存储开发模式特定设置的应用中,也可以根据调试开关覆盖现有的应用设置。

      这是一个简短的控制台应用示例(DevModeDependencyTest):

      App.config:

      <?xml version="1.0" encoding="utf-8"?>
      <configuration>
        <configSections>
          <sectionGroup name="DevModeSettings">
            <section name="debug" type="DevModeDependencyTest.DevModeSetting,DevModeDependencyTest" allowLocation="true" allowDefinition="Everywhere" />
            <section name="release" type="DevModeDependencyTest.DevModeSetting,DevModeDependencyTest" allowLocation="true" allowDefinition="Everywhere" />
          </sectionGroup>
        </configSections>
        <DevModeSettings>
          <debug webServiceUrl="http://myDebuggableWebService.MyURL.com" />
          <release webServiceUrl="http://myWebservice.MyURL.com" />
        </DevModeSettings>
        <appSettings>
          <add key="webServiceUrl" value="http://myWebservice.MyURL.com" />
        </appSettings>
      </configuration>
      

      存储自定义配置的对象 (DevModeSettings.cs):

       using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Configuration;
      
      namespace DevModeDependencyTest
      {
          public class DevModeSetting : ConfigurationSection
          {
              public override bool IsReadOnly()
              {
                  return false;
              }
      
              [ConfigurationProperty("webServiceUrl", IsRequired = false)]
              public string WebServiceUrl
              {
                  get
                  {
                      return (string)this["webServiceUrl"];
                  }
                  set
                  {
                      this["webServiceUrl"] = value;
                  }
              }
          }
      }
      

      访问自定义配置设置的处理程序 (DevModeSettingsHandler.cs):

       using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Configuration;
      
      namespace DevModeDependencyTest
      {
          public class DevModeSettingsHandler
          {
              public static DevModeSetting GetDevModeSetting()
              {
                  return GetDevModeSetting("debug");
              }
      
              public static DevModeSetting GetDevModeSetting(string devMode)
              {
                  string section = "DevModeSettings/" + devMode;
      
                  ConfigurationManager.RefreshSection(section); // This must be done to flush out previous overrides
                  DevModeSetting config = (DevModeSetting)ConfigurationManager.GetSection(section);
      
                  if (config != null)
                  {
                      // Perform validation etc...
                  }
                  else
                  {
                      throw new ConfigurationErrorsException("oops!");
                  }
      
                  return config;
              }
          }
      }
      

      最后是控制台应用程序的入口点 (DevModeDependencyTest.cs):

       using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Configuration;
      
      namespace DevModeDependencyTest
      {
          class DevModeDependencyTest
          {
              static void Main(string[] args)
              {
                  DevModeSetting devMode = new DevModeSetting();
      
                  #if (DEBUG)
                      devMode = DevModeSettingsHandler.GetDevModeSetting("debug");
                      ConfigurationManager.AppSettings["webServiceUrl"] = devMode.WebServiceUrl;
                  #endif
      
                  Console.WriteLine(ConfigurationManager.AppSettings["webServiceUrl"]);
                  Console.ReadLine();
              }
          }
      }
      

      【讨论】:

        【解决方案5】:

        SlowCheetah 不仅为 App.config 添加了您要求的功能,还为您项目中的任何 XML 文件添加了功能 - http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5

        【讨论】:

        • 虽然这在理论上可以回答这个问题,it would be preferable 在此处包含答案的基本部分,并提供链接以供参考。
        • 我认为这可能是最好的答案——它似乎允许每次构建配置 app.config 转换(很像 web.config 转换中的构建)。
        【解决方案6】:

        我有一个类似的问题要解决并最终使用 XDT (web.config) 转换引擎,这已在 ne1410s 的答案中提出,可在此处找到:https://stackoverflow.com/a/27546685/410906

        但我没有按照他的链接中描述的手动操作,而是使用了这个插件:https://visualstudiogallery.msdn.microsoft.com/579d3a78-3bdd-497c-bc21-aa6e6abbc859

        插件只是帮助设置配置,不需要构建,解决方案可以在其他机器或构建服务器上构建,无需插件或任何其他工具。

        【讨论】:

          猜你喜欢
          • 2015-08-27
          • 2013-03-19
          • 2017-08-16
          • 2014-10-21
          • 1970-01-01
          • 2014-09-16
          • 2020-01-24
          • 2011-06-02
          • 2010-12-06
          相关资源
          最近更新 更多