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

  • Creating Reactive Applications in .NET

    In modern applications, data is being retrieved in asynchronous, real-time streams, as traditional pull requests where the clients asks for data from the server are becoming a thing of the past.

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

Subscribe on YouTube