Different web.config's for development and production

K

Klaus Jensen

Hi!

I have a pretty traditional setup where I develop on my local PC and the use
"Copy Project" to deploy to the production enviroment.. In web.config I need
different values for connectionstrings etc for development and production -
pretty normalt stuff.

Currently I edit the web.config manually before deploying, and have never
forgotten - but I know it is a matter of time before I or a colleague do.

I know this is a pretty common challenge, so I know there must be an easy
solution. In my search for this solution, I found something called
Configuration Override File. This seems to be what I am looking for - or? It
does not mention whether this is for the "copy project"-function or some
other deployment method I am not aware of. I have not been able to find more
info on this subject - so I turn to you, ladies and gentlemen! :)

How do I keep two different web.config's for development and production? And
deply the correct web.config in the right case without me having to change
anything manually - using the "Copy Project"-function?

Thanks in advance :)

- Klaus Jensen
 
S

Stig Johansen

We use a session variable set in the global.asax like:
If Server.MachineName = "DevelopmentServer" Then
Session("Develop") = True
Else Session("Develop") = False

In your code you can then use this to read the correct
connection settings from web.config
 
M

Munsifali Rashid

Howdy,

My approach was to create a custom configuration class, though the problem
with this is, that you need to create an instance of the class to access the
variables.


Web.config is modified as follows:


<!-- Define our custom section names -->
<configSections>
<sectionGroup name="settings">
<section name="development"
type="System.Configuration.NameValueFileSectionHandler, System,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<section name="production"
type="System.Configuration.NameValueFileSectionHandler, System,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</sectionGroup>
</configSections>


<!-- Define development and production server settings for our
application -->
<settings>
<development>
<add key="server" value="127.0.0.1" />
<add key="mailserver" value="devmailserver" />
<add key="connstring" value="devconstring" />
</development>
<production>
<add key="server" value="liveIP_notused" />
<add key="mailserver" value="livemailserver" />
<add key="connstring" value="liveconstring" />
</production>
</settings>


A custom configuration class is used to access these variables. When
instantiated, this class checks which server the files are on, and pulls the
value from the appropriate group:


/// <summary>
/// Configuration Class
/// Check if we're using the development or live server, and get the
correct settings from web config
/// </summary>
public class CustomConfigClass
{
// Declare private variables
private bool _isDev = false;
private string _strThisServer =
HttpContext.Current.Request.ServerVariables["local_addr"];
private string _strDevServer;
private string _strProductionServer;

// Settings groups
private System.Collections.Specialized.NameValueCollection DevSettings;
private System.Collections.Specialized.NameValueCollection
ProductionSettings;


public CustomConfigClass()
{
// Get this server IP address
_strThisServer =
HttpContext.Current.Request.ServerVariables["local_addr"];

// Get the development and production server settings
DevSettings = (System.Collections.Specialized.NameValueCollection)
System.Configuration.ConfigurationSettings.GetConfig("settings/development")
;
ProductionSettings = (System.Collections.Specialized.NameValueCollection)
System.Configuration.ConfigurationSettings.GetConfig("settings/production");

// Get the development server IP
_strDevServer = DevSettings["server"];

// Get the production server IP
_strProductionServer = ProductionSettings["server"];

_isDev = (_strDevServer.IndexOf(_strThisServer) != -1);
}

/// <summary>
/// Get a value from web config
/// </summary>
/// <param name="pKey">Key</param>
/// <returns>string</returns>
public string Get(string pKey)
{
string strReturn;
if (this.IsDevServer)
{
strReturn = DevSettings[pKey];
}
else
{
strReturn = ProductionSettings[pKey];
}
return strReturn;
}
/// <summary>
/// This server's IP address
/// </summary>
public string ThisServer
{
get
{
return _strThisServer;
}
}
/// <summary>
/// Is development server
/// </summary>
public bool IsDevServer
{
get
{
return _isDev;
}
}
/// <summary>
/// Development server IP address
/// </summary>
public string DevServer
{
get
{
return _strDevServer;
}
}
/// <summary>
/// Production server IP address
/// </summary>
public string ProductionServer
{
get
{
return _strProductionServer;
}
}
}


It works quite well, though it's a bit of a hassle having to instantiate the
custom configuration class to access the settings. One of these days, I'll
re-write it to use static methods :)

Hope this helps,

Mun
 
K

Klaus Jensen

Klaus Jensen said:
How do I keep two different web.config's for development and production? And
deply the correct web.config in the right case without me having to change
anything manually - using the "Copy Project"-function?

Come on! Somebody else but me must have faced this challenge! :)

- Klaus
 
A

Anatoly

Yes.
We keep different setting in machine.config on development and production.
 
J

John Saunders

Klaus Jensen said:
Come on! Somebody else but me must have faced this challenge! :)

I use user.config for <appSettings> and maintain web.config at the
production configuration. I make few changes in the Development environment,
so I just make those by hand.
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top