我需要在一个WCF主机使用的web.config文件和一个Web客户端使用的web.config文件之间复制一些设置(例如连接字符串)。
为了避免重复,我能否让两个web.config文件都从一个单独的xml文件中读取?这两个web.config文件可以完全处于不同的解决方案/项目中,所以我认为这是不可能的,但想听听其他人的意见。
顺便说一句:我知道我可以使用数据库存储所有配置设置。
<appSettings>
,<connectionStrings>
等许多内容。<configuration>
<appSettings configSource="appSettings.config" />
<connectionStrings configSource="connectionStrings.config" />
<system.web>
<pages configSource="pages.config" />
<httpHandlers configSource="httphandlers.config">
</system.web>
</configuration>
外部化配置文件仅包含其中一个子部分:
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>
请注意,您不能将整个<system.web>
部分外部化,因为它是一个配置节组而不是配置节,但您可以外部化包含在system.web中的大多数子部分。
一个配置文件可以指向其他配置文件,只要这些文件在同一个路径下(包括子目录)。
以下是我的配置设置示例:
<connectionStrings configSource="web\config\connectionStrings.config" />
<appSettings configSource="web\config\appSettings.config" />
<system.diagnostics configSource="web\config\diagnostics.config" />
<system.serviceModel>
<bindings configSource="web\config\serviceModelBindings.config" />
<behaviors configSource="web\config\serviceModelBehaviors.config" />
<services configSource="web\config\serviceModelServices.config" />
<client configSource="web\config\serviceModelClient.config" />
</system.serviceModel>
在我的情况下,我有几个Windows应用程序在一个根目录中,其中包括一个Web应用程序作为子文件夹。这样可以让每个应用程序的配置文件指向共享的配置信息。你可以在web.config之外指定你的连接字符串http://blog.andreloker.de/post/2008/06/Keep-your-config-clean-with-external-config-files.aspx,但它必须在同一个目录下。