18

I have a SOAP Webservice that is available on multiple servers, thus having multiple endpoints. I want to avoid adding multiple Service References (C# SOAP Port Clients) with different names just to talk to this services, since the API is exactly the same.

Is there a way to configure the Endpoint URI at runtime?

beberlei
  • 4,297
  • 1
  • 23
  • 25

2 Answers2

26

I use the following which works great:

        ServiceReference1.wsSoapClient ws= new ServiceReference1.wsSoapClient();
        ws.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://xxx/myservice.asmx");
Jimbo
  • 22,379
  • 42
  • 117
  • 159
  • I get the following error, when I try this with different endpoints: "Unrecognized message version." You got any idea why? – Preexo Apr 04 '13 at 09:30
  • One additional question, should I **(A)** create a class instance of `wsSoapClient()` and use it every time I want to call a WebMethod or **(B)** use a `using` statement to instantiate on demand and free it as soon as possible? – dialex Feb 24 '16 at 17:54
5

I had trouble finding this one also. I finally just borrowed the configuration binding and did this:

private static wsXXXX.IwsXXXXClient wsXXXXClientByServer(string sServer)
{
    // strangely, these two are equivalent
    WSHttpBinding binding = new WSHttpBinding("WSHttpBinding_IwsXXXX");
    // WSHttpBinding binding = new WSHttpBinding(SecurityMode.Message, false);

    EndpointAddress remoteAddress = new EndpointAddress(new Uri(string.Format("http://{0}:8732/wsXXXX/", sServer)), new UpnEndpointIdentity("PagingService@rl.gov"));

    return new wsXXXX.IwsXXXXClient(binding, remoteAddress);
}
BillJam
  • 221
  • 3
  • 13