I have to duplicate some settings (like connection string) between a web.config file that a WCF host uses and a web.config file that a web client uses.
In the interest of not duplicating, can I have both the web.configs read from a separate xml file? The two web.configs can be entirely in different solutions/projects so I guess this is not possible, but wanted to get other’s opinion.
PS: I do understand I can use a database to store all the config settings.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Yes, any configuration section can be “externalized” – this includes things like <appSettings>, <connectionStrings> and many more.
You’d have something like this in your web.config:
<configuration>
<appSettings configSource="appSettings.config" />
<connectionStrings configSource="connectionStrings.config" />
<system.web>
<pages configSource="pages.config" />
<httpHandlers configSource="httphandlers.config">
</system.web>
</configuration>
The externalized config’s would just contain that one subsection in them:
httphandlers.config:
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
</httpHandlers>
Note you cannot externalize the entire <system.web> part, since that is a configuration section group – not a configuration section – but you can externalize most of the sub-sections contained in system.web.
Method 2
A config file can point to other config files as long as the files are in the same path (including subdirectories).
Here is an example of my config settings:
<connectionStrings configSource="webconfigconnectionStrings.config" />
<appSettings configSource="webconfigappSettings.config" />
<system.diagnostics configSource="webconfigdiagnostics.config" />
<system.serviceModel>
<bindings configSource="webconfigserviceModelBindings.config" />
<behaviors configSource="webconfigserviceModelBehaviors.config" />
<services configSource="webconfigserviceModelServices.config" />
<client configSource="webconfigserviceModelClient.config" />
</system.serviceModel>
In my case, I have several windows applications in a root folder which include a web application as a subfolder. This allows each application’s config file to point to the shared configs.
Method 3
if you can, just put the configurations in common in the Machine.Config. They both can read from there, and each web.config can override any values you need.
I say if you can, because I know that many hosting companies won’t let you modify it unless you are renting a dedicated server.
Method 4
you can specify your connectionstrings outside of your web.config
http://blog.andreloker.de/post/2008/06/Keep-your-config-clean-with-external-config-files.aspx
but it has to be in the same directory.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0