Monday, July 22, 2013

Dynamic Web Reference URLs

I inherited a number of user controls which had hard coded switches for a WCF service (live and test versions of the same service). Testing became frustrating, as code would need to be commented/uncommented in many different locations. There was also the risk of having the live serice in use on the test environment and the test service in use in the live environment. The solution was to add the URLs for the live/test references, as well as a bool for test mode into the web.config file. A switch was then used to decide which mode was active (live or test) and using the 'Url' property of the client the correct reference URL would be used.
MyClient client = new MyClient();
client.Url = "http://service.url";
The live web reference still needed to be added to the solution and a reference to it in the relevant files to ensure the correct classes/methods were available. However in this case the live/test services were identical.

No comments:

Post a Comment