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

Featured

  • Stone Steps Graphic

    Microsoft Research's SandDance Data Visualization Tool Goes Open Source

    A data visualization tool some four years in the making from Microsoft Research has been open sourced, available for use as an extension for Visual Studio Code or Azure Data Studio.

  • Writing the Code for a gRPC Service and Client in ASP.NET Core 3.0

    Once you've got a contract that describes a gRPC service, creating the service itself and a client that can call the service is easy. In fact, Visual Studio will do most of the work for you ... once you've got your projects set up correctly, that is.

  • Microsoft Doubles Down on Instructional Videos: Xamarin and Python

    After Microsoft's Scott Hanselman introduced a bunch of new beginner-level instructional videos for .NET, Xamarin guru James Montemagno wanted to remind mobile developers that similar resources are available for them.

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.

Upcoming Events