Forum
 
ForumForumDiscussions and...Discussions and....Net framework ....Net framework ...HowTo: Split a config file into manageable sectionsHowTo: Split a config file into manageable sections
Previous Previous
 
Next Next
New Post
 18/03/2008 11:44
 

config - especially web.config - files, have a habit of growing "like Topsy".  They are also a source of inappropriate coupling.  It is considered good practice to store database connection strings in the config file.  However the database connection information really only applies to one execution environment.  For example - the development connection strings should not be migrated into production; yet other config information should.

A config file may refer to another via the configSource attribute.  For example to separate the connection strings out to an independent file one would write...

 <connectionStrings configSource="connections.xml">
 </connectionStrings>

and in the file connections.xml one would write...

<?xml version="1.0"?>
<connectionStrings>
  <add name="Blah" providerName="xyz" connectionString="The string"/>
</connectionStrings>

This attribute makes it easy to separate the development environment connection string(s) from the production ones.  Of course the procedure that migrates an application from development to production must now allow for files such as connections.xml NOT to get migrated with the rest of the application.

 


Cheers,
- Richard
If this post helped you over a problem, or taught you something new, please login and rate it. Ratings are in the drop down in the top left corner
New Post
 18/03/2008 22:35
 

Also, if you're storing sensitive data in your connection strings, like unencrypted passwords, then you could put XML config files in App_Data so they can't be downloaded.  Or rename the extension to connections.config so IIS routes any requests to aspnet_isapi.dll, which will deny access.

A good way to manage connection strings like this is to create a different "configSouce" file for each deployment, e.g.

  • connections.config.dev
  • connections.config.staging
  • connections.config.production

Then for each of your build configurations create a pre-build event that renames the corresponding file to connections.config.  So, for example, when you do a Release build, connections.config.production will be renamed as connections.config and picked up by web.config through the connectionstrings configSource to point your app at the correct database.

Chris

Previous Previous
 
Next Next
ForumForumDiscussions and...Discussions and....Net framework ....Net framework ...HowTo: Split a config file into manageable sectionsHowTo: Split a config file into manageable sections

Forum Usage Guidelines

The forums are a place for all to exchange ideas and techniques, and to post and answer questions.  All are welcome to read, registration is required to post. 

If you learn somthing new, discover or acquire a new technique, then please take a moment to register and rate the post that just helped you.  This site does not send spam and it does not release your personal details.  Full details in the site privacy policy.

We have some simple posting guidelines to keep the forums a pleasant and informative environment.

  • No flames, no trolls
  • No profanity, no racism
  • Site management has the final word on approving or removing any thread, post, or comment
  • English language only please

 

Copyright 2002-15 by Dynamisys Ltd