web.config可以从外部xml文件中读取吗?

Dot*_*ude 18 asp.net wcf

我必须复制WCF主机使用的web.config文件和Web客户端使用的web.config文件之间的某些设置(如连接字符串).

为了不重复,我可以从单独的xml文件中读取web.configs吗?两个web.configs可以完全在不同的解决方案/项目中,所以我想这是不可能的,但是想得到别人的意见.

PS:我知道我可以使用数据库来存储所有配置设置.

mar*_*c_s 19

是的,任何配置部分可以被"外部化" -这包括像<appSettings>,<connectionStrings>等等.

你的web.config中有这样的东西:

<configuration>
   <appSettings configSource="appSettings.config" />   
   <connectionStrings configSource="connectionStrings.config" />
   <system.web>    
      <pages configSource="pages.config" />
      <httpHandlers configSource="httphandlers.config">
   </system.web>    
</configuration>
Run Code Online (Sandbox Code Playgroud)

外化配置只包含其中的一个子部分:

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>
Run Code Online (Sandbox Code Playgroud)

请注意,您无法外部化整个<system.web>部分,因为这是一个配置节组 - 而不是配置节 - 但您可以外部化system.web中包含的大多数子节.


Tom*_*ers 11

只要文件位于同一路径(包括子目录),配置文件就可以指向其他配置文件.

以下是我的配置设置示例:

<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>
Run Code Online (Sandbox Code Playgroud)

在我的例子中,我在根文件夹中有几个Windows应用程序,其中包含一个Web应用程序作为子文件夹.这允许每个应用程序的配置文件指向共享配置.