代码之家  ›  专栏  ›  技术社区  ›  Maxim Gershkovich

不同的应用程序设置取决于配置模式

  •  15
  • Maxim Gershkovich  · 技术社区  · 14 年前

    有人知道我可以在.NET应用程序中设置应用程序(或用户)级别设置的方法吗?这些设置是以应用程序当前的开发模式为条件的?IE:调试/发布

    更具体地说,我在应用程序设置中有对我的WebServices的URL引用。在释放模式中,我希望这些设置指向 http://myWebservice.MyURL.com 在调试模式中,我希望这些设置 http://myDebuggableWebService.MyURL.com .

    有什么想法吗?

    6 回复  |  直到 7 年前
        1
  •  15
  •   dotNET    7 年前

    我知道这是多年前被问到的,但为了以防万一,任何人都在寻找一个简单有效的解决方案,我使用。

    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
      }
      

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

        2
  •  16
  •   ne1410s    10 年前

    这对党来说有点晚了,但我偶然发现了一种很好的方法来执行 web.transform 途径 app.config 文件夹。(即,它利用名称空间 http://schemas.microsoft.com/XML-Document-Transform )

    我认为这是“不错的”,因为它是纯XML方法,不需要第三方软件。

    • 根据您的各种构建配置,父/默认app.config文件是从中派生的。
    • 然后,这些后代只覆盖他们需要覆盖的内容。

    在我看来,这比必须保持 x 全部复制的配置文件数,如在其他答案中。

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


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

        3
  •  6
  •   Tot Zam    7 年前

    据我所知,这样做没有内在的方法。在我们的项目中,我们维护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.xml , settings.settings.Release.xm 等等。

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

        4
  •  4
  •   jadusty    14 年前

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

    您可以在应用程序中保留存储特定于开发模式的设置的对象,或者基于调试开关重写现有的应用程序设置。

    下面是一个简短的控制台应用程序示例(devmodependencytest):

    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;
            }
        }
    }
    

    最后,您的入口指向控制台应用程序(devmodeldependencytest.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
  •  3
  •   Konrad Brodzik    12 年前

    Slowcheetah不仅为app.config添加了所需的功能,还为项目中的任何XML文件添加了所需的功能。- http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5

        6
  •  1
  •   Community CDub    7 年前

    我有一个类似的问题要解决,最终使用了xdt(web.config)转换引擎,NE1410的答案中已经给出了这个问题的建议,可以在这里找到: https://stackoverflow.com/a/27546685/410906

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

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