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

时间:2011-06-28 17:53:23

标签: .net configuration web-config app-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>

config2.config

<configuration>
    <system.serviceModel>
     <client>
          <endpoint name="MyEndpoint" address="net.tcp://...."
            behaviorConfiguration="MyBehavior" binding="netTcpBinding"
            contract="IMyContract">
          </endpoint>
     </client>
    </system.serviceModel>
</configuration>

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

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

1 个答案:

答案 0 :(得分:0)

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