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" />
</connectionStrings>

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

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube