Deploying ASP.NET applications without overwriting config values


Recently, I have seen quite a few questions about deploying applications. In general, the poster wants to know how to push a file out to production without overwriting values that are different.
 
In repsonse, one person answered to remove web.config from the publish directory. Now, this will work, but there are times you need to send out an updated web.config (for example, moving from 2.0 to 3.5 or adding AJAX, etc.). In these cases, you end up with a lot of editing to the production file, which is not fun.
 
If exclude is your best option, there is an easier way to do this. If you will put your connection strings and app settings in a separate file, you will find you can save yourself a lot of heartaches. Let’s take this config file as an example:
 
  <appSettings>
    <
add key="someKey" value="Somevalue"/>
  </
appSettings>
  <
connectionStrings>
    <
add name="MyConnectionString" connectionString="Server=(local);Database=MyDatabase;UID=sa;PWD=;" />
  </
connectionStrings>
 
Our first change is to add files. I am going to add AppSettings.config
 
<appSettings>
  <
add key="someKey" value="Somevalue"/>
</
appSettings>
 
and db.config
 
<connectionStrings>
  <
add name="MyConnectionString" connectionString="Server=(local);Database=MyDatabase;UID=sa;PWD=;" />
</
connectionStrings>
 
This is how you hook things up.
 

  <appSettings configSource="appSettings.config">
    <
add key="someKey" value="Somevalue"/>
  </
appSettings>
  <
connectionStrings configSource="db.config">
    <
add name="MyConnectionString" connectionString="Server=(local);Database=MyDatabase;UID=sa;PWD=;" />
  </
connectionStrings>

Now, you will still have to add values on the server’s config file(s)  each time you add an app setting or a connection string, but you can now publish the site and deploy via xCopy without fear of overwriting the server values. In addition, if you change an important config element, you can deploy the new web.config file without overwriting local values. You still have to exclude the config files from the publish or deploy, however.
 
There is a variation of config source you should know about (or a different attribute) that allows you to take this one step further. If you do the following:
 
  <appSettings file="appSettings.config">
    <
add key="someKey" value="Somevalue"/>
  </
appSettings>
  <
connectionStrings file="db.config">
    <
add name="MyConnectionString" connectionString="Server=(local);Database=MyDatabase;UID=sa;PWD=;" />
  </
connectionStrings>
 
You can now delete the files on the development machine.
 
Now, you may be asking "did he say I could delete that file locally?" and thinking "won’t it error because it cannot find that file?". The answer is yes and no. Yes, I did say exclude or delete. No, it will not error, as .NET has a built in fall back to look at web.config if it cannot find the file specified in the file attribute.
 
Note that you can put all of the values in a single config file, if you wish. I like multiple config files, as it makes it easy for me to change the value I need to change without going through a large file. Pretty kewl, eh?
 
Most of the elements in .config can be placed in their own file. Any time you find configSource as an attribute on a config element, you can place it in its own file.
 
Peace and Grace,
Greg

Twitter: @gbworld

Advertisements

2 Responses to Deploying ASP.NET applications without overwriting config values

  1. Ananth Ramasamy Meenachi says:

    Superb ! I am using your first part of your blog already, but later part is new to me.
     
    I am still not clear here(might have not read properly), after removing web.config
    from the publish directory How does it works fine ?

  2. Gregory says:

    When you remove from publish and publish over the diretory, the deploy should overwrite what is necessary and not affect config. I may have to revisit this, as I think parts do not work 100%.
     
    NOTE: While this is not useful now, it appears from reports that VS 2010 will have some new publish options that may help in this area. It will be exciting to see where they take it.
     
    Peace and Grace,Greg

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: