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):
<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
source
share