Export Your ASP.NET Configuration To Reduce Restarts, Share Settings

Practical .NET

Export Your ASP.NET Configuration To Reduce Restarts, Share Settings

You don’t have to keep all your configuration settings in your config file. There are even some benefits to exporting sections to separate files.

If you’re looking to move some of your configuration file settings out of your config file and into a separate file, you can use either the:

  • configSource attribute on many (but not all) sections
  • file attribute on appSettings section

For example, you can add the configSource attribute to your connectionStrings section to move your connection strings to a separate file (called connstrings.config in this example):

<connectionStrings configSource="connstrings.config"/>

The connstrings.config file’s contents might look something like this (notice that it looks like a complete section):

<connectionStrings >
   <add name="CustomerOrdersEntities" connectionString="..." 
        providerName="System.Data.SqlClient" />

The file referred to in configSource must be in the same folder as (or a subfolder of) your configuration file. You’ll also need to set the file’s Copy to Output Directory property to either Copy Always or Copy if Newer (the default is, I believe, Do Not Copy). You also don’t want to use configSource with a section that contains its own elements — the elements in the configSource file will completely replace the section.

If you were hoping that this would allow you to share settings among projects then you will have been disappointed that the configSource file must be in the same folder tree as your main config file. However, there is one benefit to using the configSource attribute: Changes to the configSource file will not cause the application to restart. (You can change that by setting RestartOnExternalSettings option in your machine.config file…but why would you want to?)

If you do like the idea of sharing config file settings among applications, you can do that with the appSettings section by using a different attribute: file. The file attribute allows you to refer to a file located anywhere you want, allowing many applications to point to the same file. In addition, the settings from the file in the file attribute don’t replace the appSettings section in your application’s config file.

Instead, the two sets of settings are merged (settings with different keys are added; settings with matching keys are overridden by ones from the external file). Like configSource, changes to the shared file will not cause your application to restart and the contents of the file must be a complete appSettings element.

About the Author

Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.

Bir Cevap Yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir