根据配置模式不同设置不同的应用程序设置。

21

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

更具体地说,我在我的应用设置中保存了一个指向我们Web服务的URL引用。在发布模式下,我希望这些设置指向 http://myWebservice.MyURL.com ,在调试模式下,我希望这些设置指向http://myDebuggableWebService.MyURL.com

有什么想法吗?

6个回答

18

虽然有些晚,但我找到了一种实现web.transform方法来处理app.config文件的好方法。 (即利用命名空间http://schemas.microsoft.com/XML-Document-Transform)

我认为它是“好”的,因为它是一种纯xml方法,不需要第三方软件。

  • 父级/默认的App.config文件根据各种构建配置进行派生。
  • 这些子级只覆盖它们需要的部分。

在我看来,这比维护x个完整复制的配置文件要高级和健壮得多,例如其他答案中所述。

这里发布了一篇演示文章: http://mitasoft.wordpress.com/2011/09/28/multipleappconfig/


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


已解决,谢谢!如果您使用的是VS 2017并出现了找不到Web*.targets的错误,请参考此答案https://dev59.com/TFcP5IYBdhLWcg3worn_#45354395。 - Patrick
1
对于VS 2017,在<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v15.0\Web\Microsoft.Web.Publishing.targets" />中,必须将v10.0替换为v15.0 - Jean-François Beauchamp
2
使用 Project=”$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.targets” 适用于任何 Visual Studio 版本(来自链接中的注释)。 - Papa Mufflon
1
现在有一个适用于VS的插件:Configuration Transform - StackOverthrow
这在 .net core 中可行吗?对我来说不起作用,看起来调试和发布文件从未被使用。 - IronHide

15

我知道这个问题已经问了很多年了,但以防万一有人在寻找我使用的简单有效的解决方案。

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

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

  3. 在构造函数中键入以下内容。

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

  6. 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”的配置,我该如何检查它是否是当前配置? - Steve Smith
不确定我是否理解您的意思,但是有一个#if(DEBUG)预处理器指令用于区分DEBUG和RELEASE配置。此外,您可以为每个配置定义自己的编译符号,并在#if中使用它们。 - dotNET
谢谢。我不确定你的“DEBUG”是从哪里来的。 - Steve Smith

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.xm等。
Scott Hanselman描述了一个稍微“更智能”的方法,唯一的区别是我们没有检查文件是否已更改: http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx

4
如果您想将所有内容保存在一个配置文件中,可以向您的app.settings引入自定义配置部分,以存储调试和发布模式下的属性。
您可以将存储开发模式特定设置的对象保留在您的应用程序中,或者根据调试开关覆盖现有的appsetting。
这是一个简短的控制台应用程序示例(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();
        }
    }
}

3

1
虽然这理论上回答了问题,但最好在此处包含答案的基本部分,并提供参考链接。 - Spontifixus
我认为这可能是最好的答案 - 它似乎允许每个构建配置app.config转换(就像内置的web.config转换一样)。 - David Faivre

1

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

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接