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

  • Hands On: New VS Code Insiders Build Creates Web Page from Image in Seconds

    New Vision support with GitHub Copilot in the latest Visual Studio Code Insiders build takes a user-supplied mockup image and creates a web page from it in seconds, handling all the HTML and CSS.

  • Naive Bayes Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the naive Bayes regression technique, where the goal is to predict a single numeric value. Compared to other machine learning regression techniques, naive Bayes regression is usually less accurate, but is simple, easy to implement and customize, works on both large and small datasets, is highly interpretable, and doesn't require tuning any hyperparameters.

  • VS Code Copilot Previews New GPT-4o AI Code Completion Model

    The 4o upgrade includes additional training on more than 275,000 high-quality public repositories in over 30 popular programming languages, said Microsoft-owned GitHub, which created the original "AI pair programmer" years ago.

  • Microsoft's Rust Embrace Continues with Azure SDK Beta

    "Rust's strong type system and ownership model help prevent common programming errors such as null pointer dereferencing and buffer overflows, leading to more secure and stable code."

  • Xcode IDE from Microsoft Archrival Apple Gets Copilot AI

    Just after expanding the reach of its Copilot AI coding assistant to the open-source Eclipse IDE, Microsoft showcased how it's going even further, providing details about a preview version for the Xcode IDE from archrival Apple.

Subscribe on YouTube

Upcoming Training Events