The first time I tried to write a web service client in Visual Studio 2010, my instinct said “create a service reference”. After all, Microsoft loves to tie things in through references. So I did… and I quickly realized I was going to have to create a service reference for each environment if I was going to use the same client for accessing endpoints in multiple environments.
A better way to go is to jump outside Visual Studio and use the command line application wsdl.exe.
wsdl /out:WSProxy.cs http://domain/MyWebService/Endpoint.asmx?WSDL
The stub this lovely little creates has a constructor that directly sets the URL.
public MyWebService() { this.Url = "http://localhost/MyWebService/Endpoint.asmx"; }
Add an override constructor to provide your own URL to make this class more diverse.
public MyWebService(string url) { this.Url = url; }
Then, get really lazy like me and add a switch based on a command line arg.
if (arg1.Equals("LOCAL")) proxy = new WSProxy() else if (arg1.Equals("QA")) proxy = new WSProxy("http://domain/MyWebService/Endpoint.asmx");