根据配置模式不同的应用程序设置
有谁知道我可以在 .Net 应用程序中设置应用程序(或用户)级别设置的方法,这些设置以应用程序当前的开发模式为条件? IE:调试/发布
更具体地说,我有一个对应用程序设置中保存的 Web 服务的 url 引用。在发布模式下,我希望这些设置指向 http://myWebservice.MyURL.com 在调试模式下我会喜欢这些设置 http://myDebuggableWebService.MyURL.com。
有什么想法吗?
Is anyone aware of a way that I can set application (or user) level settings in a .Net application that are conditional on the applications current development mode? IE: Debug/Release
To be more specific, I have a url reference to my webservices held in my application settings. During release mode I would like those settings to point to http://myWebservice.MyURL.com during debug mode I would love those settings to be http://myDebuggableWebService.MyURL.com.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这有点晚了,但我偶然发现了一种为
app.config
文件实现web.transform
方法的好方法。 (即它使用命名空间http://schemas.microsoft.com/XML-Document-Transform
)我认为它“很好”,因为它是一种纯 xml 方法并且不不需要第 3 方软件。
在我看来,这比必须维护
x
个完整复制的配置文件(例如其他答案中的配置文件)更加复杂和强大。发布在这里:
http://mitasoft.wordpress.com/2011/09/28/multipleappconfig/
看,妈妈 - 我的 IDE 中没有明确的构建后事件!
This is somewhat late to the party, but I stumbled upon a nice way of implementing the
web.transform
approach forapp.config
files. (i.e. it makes use of the namespacehttp://schemas.microsoft.com/XML-Document-Transform
)I think it is "nice" because it is a pure xml approach and doesn't require 3rd party software.
In my opinion this is much more sophisticated and robust than having to maintain
x
number of config files which get copied in their entirety, such as in other answers.A walkthrough has been posted here:
http://mitasoft.wordpress.com/2011/09/28/multipleappconfig/
Look, Mom - No explicit post-build events in my IDE!
我知道这是几年前提出的问题,但以防万一有人正在寻找我使用的简单有效的解决方案。
转到项目属性的“设置”选项卡(您将看到您的 Web 服务 URL 或此处已列出的任何其他设置)。
转到项目属性的
单击“设置”页面上的“查看代码”按钮。
在构造函数中键入此内容。
在构造函数下添加以下函数:
现在,每当您运行项目时,它将仅编译与当前构建配置匹配的行。
I know this was asked years ago, but just in case anyone is looking for a simple and effective solution that I use.
Go to project properties, Settings tab (you'll see your web service URL or any other settings already listed here).
Click the "View Code" button available on the Settings page.
Type this in the constructor.
Add the following function under the constructor:
Now whenever you run your project, it will compile only the line that matches the current build configuration.
据我所知,没有内置的方法可以做到这一点。在我们的项目中,我们维护 4 个不同的设置文件,并通过在构建的预构建步骤中将每个文件复制到活动文件中来在它们之间进行切换。
几年来,这对我们来说一直完美无缺。只需更改目标,整个配置文件也会随之切换。
编辑:这些文件被命名为例如
settings.settings.Debug.xml
、settings.settings.Release.xm
l等。ScottHanselman有描述了一种稍微“聪明”的方法,唯一的区别是我们没有检查文件是否已更改:
http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx
There is, as far as I know, no built in way of doing this. In our project we maintain 4 different settings files, and switch between them by copying each into the active file in the prebuild step of the build.
This has worked flawlessly for us for a few years. Simply change the target and the entire config file is switched as well.
Edit: The files are named e.g.
settings.settings.Debug.xml
,settings.settings.Release.xm
l etc..Scott Hanselman has described a slightly 'smarter' approach, the only difference is that we don't have the check to see if the file has changed:
http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx
如果您想将所有内容保留在一个配置文件中,您可以在 app.settings 中引入一个自定义配置部分来存储调试和发布模式的属性。
您可以将存储开发模式特定设置的对象保留在应用程序中,也可以根据调试开关覆盖现有的应用程序设置。
以下是一个简短的控制台应用程序示例 (DevModeDependencyTest):
App.config:
用于存储自定义配置的对象 (DevModeSettings.cs):
用于访问自定义配置设置的处理程序 (DevModeSettingsHandler.cs):
最后是控制台的入口点应用程序(DevModeDependencyTest.cs):
If you want to keep everything in one configuration file you can introduce a custom configuration section to your app.settings to store properties for debug and release modes.
You can either persist the object in your app that stores dev mode specific settings or override an existing appsetting based on the debug switch.
Here is a brief console app example (DevModeDependencyTest):
App.config :
The object to store your custom configuration (DevModeSettings.cs):
A handler to access your custom configuration settings (DevModeSettingsHandler.cs) :
And finally your entry point to the console app (DevModeDependencyTest.cs) :
SlowCheetah 添加的功能不仅适用于 App.config,还适用于项目中的任何 XML 文件 - http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5
SlowCheetah adds the functionality you ask for not only for App.config but for any XML file in your project - http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5
我有一个类似的问题需要解决,最终使用了 XDT (web.config) 转换引擎,ne1410s 的答案中已经建议了这一点,可以在这里找到: https://stackoverflow.com/a/27546685/410906
但是我没有按照他的链接中所述手动执行此操作,而是使用了此插件:https://visualstudiogallery.msdn.microsoft.com/579d3a78-3bdd-497c-bc21-aa6e6abbc859
该插件仅帮助设置配置,不需要构建,并且可以在其他计算机或构建服务器上构建解决方案,而无需插件或任何其他工具。
I had a similar problem to solve and ended up using the XDT (web.config) transform engine, that was already suggested in the answer from ne1410s that can be found here: https://stackoverflow.com/a/27546685/410906
But instead of doing it manually as described in his link I used this plugin: https://visualstudiogallery.msdn.microsoft.com/579d3a78-3bdd-497c-bc21-aa6e6abbc859
The plugin is only helping to setup the configuration, it's not needed to build and the solution can be built on other machines or on a build server without the plugin or any other tools being required.