.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:

<appSettings>
   <add key="Location" value="dev"/>
   <add key="dev.ConnectionString"
        value="server=(localhost);database=mydb;
               uid=sa;pwd=sapassword"/>
   <add key="prod.ConnectionString"
        value="server=prodserver;database=mydb;
               uid=prodserver_user;pwd=produser_pwd"/>
</appSettings>

You then would create a function to wrap the ConfigurationManager class to determine which settings to retrieve, based on the Location setting:

private string GetSetting(string setting)
{
   string location = ConfigurationManager.AppSettings["Location"];
   return ConfigurationManager.AppSettings[location + "." + setting];
}

Using this function is pretty easy:

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.

About the Author

Eric Smith is the owner of Northstar Computer Systems, a Web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic. Send him your questions and feedback via e-mail at questions@techniquescentral.com.



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • Live Event Date: September 17, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this upcoming eSeminar and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds