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"/>
</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