I want to override client WCF endpoint addresses stored in app.config so that I can change them from pointing to "localhost" to pointing to production URLs [depending on configuration that can be set from within the App (contained in an object 'appConfig' in the code shown below) - which is a WinForms project.]
By reading other questions in this area I have reached the following pieces of code (InitAllEndpoints which calls into InitEndpoint) which I call from the Form_Load event. I tried these in my application and they appear to change the EndPoint addresses if I hover over the value in the "ep" variable. Yet if I loop again through serviceModelSectionGroup.Client.Endpoints after my code I find they are infact unchanged. (I now read that EndPoint addresses are immutable - so my code looks wrong anyway as I'd expect to overwrite the address with a new EndPoint address object - rather than a Uri?)
How do I can I programmatically override client app.config WCF endpoint addresses?
private void InitAllEndpoints()
{
ServiceModelSectionGroup serviceModelSectionGroup =
ServiceModelSectionGroup.GetSectionGroup(
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None));
if (serviceModelSectionGroup != null)
{
foreach (ChannelEndpointElement ep in serviceModelSectionGroup.Client.Endpoints)
{
InitEndpoint(ep,
appConfig.ExternalComms_scheme,
appConfig.ExternalComms_host,
appConfig.ExternalComms_port);
}
}
}
private void InitEndpoint(ChannelEndpointElement endPoint, string scheme, String host, String port)
{
string portPartOfUri = String.Empty;
if (!String.IsNullOrWhiteSpace(port))
{
portPartOfUri = ":" + port;
}
string wcfBaseUri = string.Format("{0}://{1}{2}", scheme, host, portPartOfUri);
endPoint.Address = new Uri(wcfBaseUri + endPoint.Address.LocalPath);
}
Note: My proxies lives in a separate project/DLL.
e.g.
public class JournalProxy : ClientBase<IJournal>, IJournal
{
public string StoreJournal(JournalInformation journalToStore)
{
return Channel.StoreJournal(journalToStore);
}
}