How to replace .NET configuration source at run time

Here is my problem. I have a group system.serviceModelwhere the sections of services and clients are outside the sources using the tool configSource. Now I have two different sets of external / client files outside the service - one set for insecure communication, and the other for secure.

I want to start the server using the command line, for example / cfgSource = Secure or / cfgSource = NonSecure (or / cfgSource = kuku, provided that there are service / client configuration files that correspond to kuku), which will effectively switch the configurations for safe or insecure mode.

This is a big picture. Now for the details. My group system.serviceModelcurrently looks like this: alt text

If the sections bindingsand behaviorsdetermine all the bindings and behavior necessary for both safe and insecure configurations servicesand client. Now I want to start the application using the command line /cfgSource=A.Plain, which will change the lines

<services configSource="Cfg\A.Secure\services.config" />
<client configSource="Cfg\A.Secure\client.config" />

to

<services configSource="Cfg\A.Plain\services.config" />
<client configSource="Cfg\A.Plain\client.config" />

So, I happily continued the configuration change, for example:

var cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var serviceModel = (ServiceModelSectionGroup)cfg.GetSectionGroup("system.serviceModel");
serviceModel.Sections["services"].SectionInformation.ConfigSource = @"Cfg\A.Plain\services.config";
serviceModel.Sections["client"].SectionInformation.ConfigSource = @"Cfg\A.Plain\client.config";
cfg.Save();

Gevalt! Not only configSource links have been changed (this is normal), but the actual files under A.Plain are overwritten in files under A.Secure! I understand that this is pretty logical, but what should I do to keep A.Plain files intact and just replace configSource links?

Thanks in advance to all the good Samaritans.

PS

. , XPATH, . - , .

EDIT1

-, configSource , , .

serviceModel.Sections["services"].SectionInformation.GetRawXml()

configSource. , , , .

+3

Source: https://habr.com/ru/post/1780372/


All Articles