Wednesday, November 14, 2007

Dynamically changing URL of a WCF proxy

A couple of weeks ago I was creating a WPF/WCF version of a very simple little application I've made that checks if all my production web services are running correctly. It's really simple, using an existing web service it retrieves the URI of all the current production services, then iterates them and calls a web service method that either returns True is the service has access to the database and all, or False if it doesn't. The little application shows the list of services with a little flag for each. The flag is green if the web service call went well and returned True, red if either the call failed ("hay caramba, time to check IIS!"), or orange if the call is pending.
Simple but very useful when the production server was going bonkers a couple of months ago.

In the old days of .Net 2.0, to change the URI of a web service proxy, you just set the "Url" property, re-set the credentials if you use them and call the method. Now with WCF, I have defined all the bindings in my configuration file, in the SOAP client's EndPoint I set the "Address" property with the URI of my server and I'm done! I don't even need to re-set the credentials that .Net 2.0 used to "forget" when I changed the URL of the proxy.

MyServiceSoapClient securitySoapClient = new MyServiceSoapClient();
for(int i=0; i<list.Count; i++)
{
mySoapClient.Endpoint.Address = new EndpointAddress(myServiceUri[list[i].Uri]);
//Call the service here
}

No comments: