有没有人知道在.Net应用程序中设置应用程序(或用户)级别的设置,这些设置取决于应用程序当前的开发模式?即:调试/发布
更具体地说,我在我的应用设置中保存了一个指向我们Web服务的URL引用。在发布模式下,我希望这些设置指向 http://myWebservice.MyURL.com ,在调试模式下,我希望这些设置指向http://myDebuggableWebService.MyURL.com。
有什么想法吗?
有没有人知道在.Net应用程序中设置应用程序(或用户)级别的设置,这些设置取决于应用程序当前的开发模式?即:调试/发布
更具体地说,我在我的应用设置中保存了一个指向我们Web服务的URL引用。在发布模式下,我希望这些设置指向 http://myWebservice.MyURL.com ,在调试模式下,我希望这些设置指向http://myDebuggableWebService.MyURL.com。
有什么想法吗?
虽然有些晚,但我找到了一种实现web.transform
方法来处理app.config
文件的好方法。 (即利用命名空间http://schemas.microsoft.com/XML-Document-Transform
)
我认为它是“好”的,因为它是一种纯xml方法,不需要第三方软件。
在我看来,这比维护x
个完整复制的配置文件要高级和健壮得多,例如其他答案中所述。
这里发布了一篇演示文章: http://mitasoft.wordpress.com/2011/09/28/multipleappconfig/
看,妈妈——我的IDE没有明确的后期构建事件!
我知道这个问题已经问了很多年了,但以防万一有人在寻找我使用的简单有效的解决方案。
进入项目属性,选择“设置”选项卡(您将看到您的Web服务URL或任何其他设置已在此处列出)。
在设置页面上点击“查看代码”按钮。
在构造函数中键入以下内容。
this.SettingsLoaded += Settings_SettingsLoaded;
在构造函数下添加以下函数:
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
}
现在,每当你运行项目时,它只会编译与当前构建配置匹配的代码行。#if(DEBUG)
预处理器指令用于区分DEBUG和RELEASE配置。此外,您可以为每个配置定义自己的编译符号,并在#if
中使用它们。 - dotNETcopy "$(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
等。<?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();
}
}
}
SlowCheetah为您的项目中的任何XML文件添加了您所需的功能,不仅适用于App.config - http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5
我有一个类似的问题需要解决,最终使用了 XDT (web.config) 转换引擎,这个引擎已经在 ne1410s 的答案中提到了,可以在这里找到: https://dev59.com/FHA75IYBdhLWcg3w0cnx#27546685
但是,我没有像他的链接中描述的那样手动完成,而是使用了这个插件: https://visualstudiogallery.msdn.microsoft.com/579d3a78-3bdd-497c-bc21-aa6e6abbc859
该插件只是帮助设置配置,不需要构建,解决方案可以在其他计算机或构建服务器上构建,而不需要插件或任何其他工具。
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v15.0\Web\Microsoft.Web.Publishing.targets" />
中,必须将v10.0
替换为v15.0
。 - Jean-François BeauchampProject=”$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.targets”
适用于任何 Visual Studio 版本(来自链接中的注释)。 - Papa Mufflon