Pages

Wednesday, August 8, 2012

.NET Tip: Managing Development and Production Configuration Files


Most developers have one server they use to develop their applications and another server where they deploy the applications. The problem with this configuration is that you typically have to change your database connection settings, file locations, and so forth, each time you deploy your files. Inevitably, you'll add or remove settings from your configuration files and those settings won't be set up properly in the production environment.
A quick way to fix this problem is to use a prefix on your configuration settings and a function to determine which settings to use at runtime. Here's an example of a configuration file's appSettings section that is set up this way:
  1. <appSettings>
  2. <add key="Location" value="dev"/>
  3. <add key="dev.ConnectionString"
  4. value="server=(localhost);database=mydb;
  5. uid=sa;pwd=sapassword"/>
  6. <add key="prod.ConnectionString"
  7. value="server=prodserver;database=mydb;
  8. uid=prodserver_user;pwd=produser_pwd"/>
  9. </appSettings>
You then would create a function to wrap the ConfigurationManager class to determine which settings to retrieve, based on the Location setting:
  1. private string GetSetting(string setting)
  2. {
  3. string location = ConfigurationManager.AppSettings["Location"];
  4. return ConfigurationManager.AppSettings[location + "." + setting];
  5. }
Using this function is pretty easy:
  1. Response.Write(GetSetting("ConnectionString"));
When you deploy your code for this example, you can copy the entire configuration file to the server and simply change the Location from 'dev' to 'prod'. It's a fairly simple way to manage multiple configurations without a lot of work. You'd obviously want to beef up the GetSetting function to make sure that the setting wasn't empty, and so on, but the concept is something you might be able to use in your own applications.

No comments:

Post a Comment