There are a lot of people having issues when they have to change the location of the web service they are calling. Some resort to recompiling the proxy class, but that isn’t necessary. The proxy class that is generated by VS hard-codes the URL of the web service in the class, but with a little foresight, you can avoid problems when the location of the web service changes. First, let’s create a configuration item to control the URL where the web service resides:
<?
xml version=”1.0″ encoding=”utf-8″ ?><configuration>
<appSettings>
<add key=”webServiceUrl” value=”http://localhost/service.asmx” />
</appSettings>
</configuration>
Now, we just add 1 line to the code that calls this web service:
myWebService.Batch service = new myWebService.Batch();
service.Url = ConfigurationSettings.AppSettings[“webServiceUrl”];
Debug.WriteLine(service.Url);
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
service.MyWebServiceMethod(textBox1.Text);
Debug.WriteLine(“Called web service”);
By setting the “Url” property, we can change where the system goes to consume the web service. Now, you can deploy the web service to any server, and if you change your app.config file, your client code won’t miss a beat.