Using different configurations (web.config, app.config)

T

thalion77

Hello there,

is there a possibility to use different configurations for debug and
release mode?

I need other settings in debug mode than in release mode but do not
want to replace the web.config all the time manually.

Regards,
Norbert
 
M

Michael Nemtsev

Hello Norbert_Pürringer,

Nope, you can't
everything you can do is to add debug keys to the config and use them if
u in debug mode

---
WBR, Michael Nemtsev [.NET/C# MVP].
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

N> Hello there,
N>
N> is there a possibility to use different configurations for debug and
N> release mode?
N>
N> I need other settings in debug mode than in release mode but do not
N> want to replace the web.config all the time manually.
N>
N> Regards,
N> Norbert
 
J

John Saunders [MVP]

Hello there,

is there a possibility to use different configurations for debug and
release mode?

One thing you can do is switch appSettings using user.config:

<configuration>
<appSettings file="user.config">
<add key="keyName" value="releaseValue"/>
</appSettings>
</configuration>

in user.config:

<appSettings>
<add key="keyName" value="debugValue"/>
<add key="otherKey" value="debugOnly"/>
</appSettings>

If user.config doesn't exist, then everything is normal. If it's present,
then its values supersede any corresponding values in the web.config.
 
C

Chris Mullins [MVP]

There are a few ways to do this that come time mind:

The first is in debug/release mode, use different key names.

You app.config would look like
<add key='option1' value='do it the easy way'/>
<add key='option1_debug' value='do it the hard way'/>

.... and inside your code, have a single entry/exit point for looking up key
value.

That code will say:
string keyValue = passedInKeyValue
#if Debug
keyValue = keyValue + "_debug";
#end if
return ConfigurationManager.AppSettings[keyValue];

That would work with no trouble.

Another option is to use a custom app domain. When you create a new
application domain, you get to specify the name & location of the config
file. You could have a "loader" app domain in your Main(), that says,
AppDomain d = new AppDomain()
#if DEBUG
d.ConfigPath = "debug.config";
#else
d.ConfigPath="release.config";
#end if

.... and all your "real" code runs in this new app domain.
 
J

John Saunders [MVP]

Chris Mullins said:
I hadn't thought of that one. That's a good way to go...

An "oldie, but a goodie", at least for appSettings.

I just took a look at Web Deployment Projects (see
http://msdn2.microsoft.com/en-us/asp.net/aa336619.aspx). Among other things,
this feature seems to allow you to replace individual sections of the
web.config file on a per-configuration basis. In particular, you can add:

appSettings="debugAppSettings.config"
system.web="debugSystem.web.config"

and reverse them for Release.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top