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?
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) :
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
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
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!
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.
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.