O'Reilly logo

ASP.NET Cookbook by Geoffrey T. LeBlond, Michael A Kittel

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

11.4. Setting the URL of a Web Service at Runtime

Problem

You need to set the URL of the web service at runtime.

Solution

In the code-behind class for your page, set the URL property of the proxy class to the required URL after instantiating the proxy object, as shown here:

               Solution
bookServices = New ExampleBookServices.CH11BookServicesVB( )
bookServices.Url = "http://michaelk/aspnetcookbook/CH11BookServicesVB.asmx"

Solution
bookServices = new ExampleBookServices.CH11BookServicesCS( );
bookServices.Url = "http://michaelk/aspnetcookbook/CH11BookServicesCS.asmx";

Better still, by storing the URL in web.config and setting the URL property of the proxy class at runtime from the web.config setting, you can avoid having to change the code whenever the URL changes.

Discussion

The ability to configure an application without having to recompile its code every time you make a change in the location of its resources can be a real time-saver. Since the URL for a web service can change, it is a good idea to code your application to set the URL at runtime.

Whenever you add a web reference to a project, Visual Studio creates a proxy class for the web service using the URL you enter. For example, the constructor for the class from Recipe 11.3 is shown here with a hardcoded value for the URL that Visual Studio added using ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required