21

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

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

有任何想法吗?

4

6 回答 6

18

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

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

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

x在我看来,这比必须维护大量复制的配置文件(例如在其他答案中)要复杂和强大得多。

演练已在此处发布:http: //mitasoft.wordpress.com/2011/09/28/multipleappconfig/


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

于 2014-12-18T12:31:29.653 回答
15

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

  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
    }
    

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

于 2012-09-18T22:48:49.557 回答
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.xmlsettings.settings.Release.xml等。

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

于 2010-06-22T05:06:06.847 回答
4

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

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

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

应用程序配置:

<?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();
        }
    }
}
于 2010-06-22T09:46:08.373 回答
3

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

于 2013-01-28T12:26:16.377 回答
1

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

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

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

于 2016-02-10T12:05:15.247 回答