0

I'm writing an application that needs to connect to a web service. Under certain circumstances, I need to toggle the endpoint address.

I assume this is as simple as changing the System.ServiceModel.Description.ServiceEndpoint, when the address needs to change. However, I'm getting an exception when I do this because one addresses requires SSL, and the other address doesn't.

How can I correctly update web services endpoint address?

Note: This is a C#, .Net 3.5 project.

RLH
  • 15,230
  • 22
  • 98
  • 182
  • Does this work for you? http://stackoverflow.com/questions/1978962/how-can-i-change-the-endpoint-address-programmatically-in-the-client-site – dugas Sep 24 '13 at 14:56
  • Sorry for my ignorance, but where does the proxy come from? – RLH Sep 24 '13 at 15:23
  • Give this a read: http://msdn.microsoft.com/en-us/library/ms733133.aspx. – dugas Sep 24 '13 at 15:43
  • Are you connecting to a wcf service or an xml web service? – dugas Sep 24 '13 at 15:50
  • It's XML, but it's an old ASP.net form of web service. – RLH Sep 24 '13 at 18:00
  • Then give this a read: http://msdn.microsoft.com/en-us/library/bb628649.aspx and change the Url property of the proxy created when you add the web reference. – dugas Sep 24 '13 at 19:15
  • I think the proxy approach is the correct way to solve this. I just need to figure out a way to do it. In the "olden days" you could just update a `Url` property on your web service class. Looks like it doesn't work that way any more. I'll try to figure this one out on my own, unless someone is aware of a simpler answer. – RLH Sep 25 '13 at 10:28

1 Answers1

0

Ok, I found the solution. When Visual Studio generates the wrapper classes for your service, the [ServiceName]SoapClient has a constructor that takes a binding and an endpoint as a parameter. Define these and just pass them to the constructor.

Here is a pseudo-example.

    void InitializeMyWebService(bool useSSLSite)
    {
        BasicHttpBinding b = useSSLSite ? 
            new BasicHttpBinding(BasicHttpSecurityMode.Transport) : 
            new BasicHttpBinding();

        EndpointAddress e = useSSLSite ? 
            new EndpointAddress("https://www.example.com/svc/MyWebService.asmx") :
            new EndpointAddress("http://intranet_server/svc/MyWebService.asmx");

        myWebService = new MyWebServiceSoapClient(b, e);
    }
}

MyWebService will now work, as defined by the userSSLSite parameter to our method.

RLH
  • 15,230
  • 22
  • 98
  • 182