Using a C# Service Reference SOAP Client with different Endpoint URIs
Asked Answered
E

2

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?

Emylee answered 9/8, 2010 at 11:42 Comment(0)
C
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");
Colis answered 20/1, 2012 at 12:52 Comment(2)
I get the following error, when I try this with different endpoints: "Unrecognized message version." You got any idea why?Polard
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?Sylvan
S
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("[email protected]"));

    return new wsXXXX.IwsXXXXClient(binding, remoteAddress);
}
Stickup answered 16/9, 2010 at 12:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.