Creating a System.Configuration.Configuration by merging two config files?
I have two .config files, and I need a System.Configuration.Configuration that is a partial merge between them?
I can read the files as XML and create the desired merge easily, but then it's a string or XDocument. However, .net's System.Configuration seems to be stricly file based, so I would have to write it to a temp directory which I'd like to avoid.
Is there a way to do that?
Example, config1.config
:
<configuration>
<appSettings>
<add key="CacheTime" value="300" />
</appSettings>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="MyBehavior">
<!-- snipped -->
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
and config2.config
:
<configuration>
<system.serviceModel>
<client>
<endpoint name="MyEndpoint" address=开发者_如何学Go"net.tcp://...."
behaviorConfiguration="MyBehavior" binding="netTcpBinding"
contract="IMyContract">
</endpoint>
</client>
</system.serviceModel>
</configuration>
The resulting Configuration should then be the union of these two, so that I have both the AppSetting and the Endpoint. Now, this example contains WCF, but I'm not looking for a WCF specific solution, as I absolutely need a System.Configuration.Configuration object.
config1 and config2 are just examples - the actual combination of them is non-deterministic, I might have config1 and config4 or config 3 and config4 or config2 and config3.
If your goal is to simply make your configuration files more modular (and potentially protect file access to particular sections), have a look at System.Configuration.SectionInformation.ConfigSource.
精彩评论