views:

23

answers:

1

I need to be able to update my config file programmatically and change my WCF settings. I've been trying to do this inside of some test code using some of the examples I found on the web but so have not been able to get the config file to reflect a change to an endpoint address.

Config (snippet):

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

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

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

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

  </service>

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 executes with no exceptions but no changes are made. Also I had trouble indexing the endpoints and services. Is there an easy way to find it? Using name as the indexer did not seem to work.

Thanks!

Sieg

+1  A: 

I changed two things, and it works just fine for me:

1) I am using OpenExeConfiguration with the assembly path

2) I'm accessing the <services> section, rather than the <system.serviceModel> section group

With those two changes, everything works just fine:

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);
marc_s
Thanks Marc. The interesting thing was that running through the ide would not reflect the changes. The more that I think about it why that is it makes sense, though it was bit confusing as I was looking at app.config for changes vs. exe.config.
Sieg
@Sieg: that was one of my initial gut feeling - are you looking at the right file? I noticed that when executing your code, what gets modified is really the `YourAppName.vshost.exe.config` but even those changes aren't actually persisted to disk for some obscure reason....
marc_s