Software Configuration Changes for WCF

I need to be able to update my configuration file programmatically and change WCF settings. I tried to do this inside some test code, using some of the examples I found on the Internet, but could not get the configuration file to reflect the change in the address of the endpoint.

Configuration (fragment):

  

  <!-- Sync Support -->
  <service   name="Server.ServerImpl"
             behaviorConfiguration="syncServiceBehavior">

    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8000/SyncServerHarness"/>
      </baseAddresses>
    </host>

    <endpoint name="syncEndPoint" 
              address="http://localhost:8000/SyncServerHarness/Sync"
              binding="basicHttpBinding"
              contract="Server.IServer" />

    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

  </service>

the code:

Configuration config = ConfigurationManager.OpenExeConfiguration
                       (ConfigurationUserLevel.None);

ServiceModelSectionGroup section = (ServiceModelSectionGroup)
                                   config.SectionGroups["system.serviceModel"];

foreach (ServiceElement svc in section.Services.Services)
{
   foreach (ServiceEndpointElement ep in svc.Endpoints)
   {
       if (ep.Name == "syncEndPoint")
       {
          ep.Address = new Uri("http://192.168.0.1:8000/whateverService");

       }
   }
}

config.Save(ConfigurationSaveMode.Full);
ConfigurationManager.RefreshSection("system.serviceModel");

This code is executed without any exceptions, but no changes are made. Also, I was not able to index the endpoints and services. Is there an easy way to find it? Using the name as an indexer does not seem to work.

Thank!

Zig

+3
source share
2 answers

, :

1) OpenExeConfiguration

2) <services>, <system.serviceModel>

:

Configuration config = ConfigurationManager.OpenExeConfiguration
                       (Assembly.GetExecutingAssembly().Location);

ServicesSection section = config.GetSection("system.serviceModel/services") 
                             as ServicesSection;

foreach (ServiceElement svc in section.Services)
{
   foreach (ServiceEndpointElement ep in svc.Endpoints)
   {
       if (ep.Name == "syncEndPoint")
       {
          ep.Address = new Uri("http://192.168.0.1:8000/whateverService");
       }
   }
}

config.Save(ConfigurationSaveMode.Full);
+2

... - , app.config RefreshSection, , , :

ConfigurationManager.RefreshSection("system.serviceModel");

, . . :

ConfigurationManager.RefreshSection("system.serviceModel/client");

(, , ,...). , . MSDN . 3 .

+1

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


All Articles