通过合并两个配置文件来创建System.Configuration.Configuration?

Mic*_*tum 5 .net configuration app-config web-config

我有两个.config文件,我需要一个System.Configuration.Configuration,它们之间是部分合并?

我可以将文件作为XML读取并轻松创建所需的合并,但它是一个字符串或XDocument.但是,.net的System.Configuration似乎是严格基于文件的,所以我必须将它写入我想避免的临时目录.

有没有办法做到这一点?

示例config1.config:

<configuration>
    <appSettings>
        <add key="CacheTime" value="300" />
    </appSettings>
    <system.serviceModel>
        <behaviors>
          <endpointBehaviors>
            <behavior name="MyBehavior">
               <!-- snipped -->
            </behavior>
          </endpointBehaviors>
        </behaviors>
    </system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)

并且config2.config:

<configuration>
    <system.serviceModel>
     <client>
          <endpoint name="MyEndpoint" address="net.tcp://...."
            behaviorConfiguration="MyBehavior" binding="netTcpBinding"
            contract="IMyContract">
          </endpoint>
     </client>
    </system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)

结果配置应该是这两者的联合,这样我就有了AppSetting和Endpoint.现在,这个例子包含WCF,但我不是在寻找一个特定于WCF的解决方案,因为我绝对需要一个System.Configuration.Configuration对象.

config1和config2只是示例 - 它们的实际组合是非确定性的,我可能有config1和config4或config 3和config4或config2和config3.

drh*_*ris 0

如果您的目标只是使配置文件更加模块化(并可能保护对特定部分的文件访问),请查看System.Configuration.SectionInformation.ConfigSource