1

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);
    }


}
Paul H
  • 303
  • 1
  • 6
  • 13

2 Answers2

5

The only way I have done this is to replace the EndpointAddress on each constructed instance of a client.

using (var client = new JournalProxy())
{
    var serverUri = new Uri("http://wherever/");
    client.Endpoint.Address = new EndpointAddress(serverUri,
                                                  client.Endpoint.Address.Identity,
                                                  client.Endpoint.Address.Headers);

    // ... use client as usual ...
}
CodingWithSpike
  • 42,906
  • 18
  • 101
  • 138
1

I accomplish modifying the endpoint of the wcf service on the client by utlizing the ClientBase<> constructor in the client proxy

MDSN - ClientBase

public class JournalProxy : ClientBase<IJournal>, IJournal 
{     

    public JournalProxy()
        : base(binding, endpointAddress)
    {
    }

    public string StoreJournal(JournalInformation journalToStore)     
    {         
        return Channel.StoreJournal(journalToStore);     
    }   
} 

In my case I create the binding and the endpoint from database settings in the client proxy, you might be able to use ClientBase(string endpointConfigurationName, string remoteAddress) instead

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
trouta
  • 426
  • 3
  • 12