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:
bookServices = New ExampleBookServices.CH11BookServicesVB( ) bookServices.Url = "http://michaelk/aspnetcookbook/CH11BookServicesVB.asmx" 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.
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 the value we entered when adding the web reference:
Public Class BookServices_VB Inherits System.Web.Services.Protocols.SoapHttpClientProtocol Public Sub New( ) MyBase.NewMe.Url = _
"http://localhost/ASPNetCookbook/VBWebServices/CH11BookServicesVB.asmx"
End Sub ... End Class public class BookServices_CS : System.Web.Services.Protocols.SoapHttpClientProtocol { public BookServices_CS( ) {this.Url =
http://localhost/ASPNetCookbook/CSWebServices/CH11BookServicesCS.asmx";
} ... }
If the URL changes and your code does not provide the ability to set the URL at runtime, we would need to delete the current web reference, add a web reference to the new URL, recompile the code, and then deploy the application. This is a lot of work simply to change a URL.
By storing the URL in a configuration file, such as
web.config, and setting the URL property of the
proxy class at runtime, no code changes are required when the URL
changes. By selecting the web reference in the Solution Explorer
window and then changing the URL
Behavior
property in the Properties window to
Dynamic
, as shown in Figure 11-5,
Visual Studio .NET alters the constructor of the proxy class to read
the URL from web.config and set the URL property
if the setting was found in web.config or use
the hardcoded value if it was not found. The following code snippets
show the code Visual Studio .NET creates for the constructor when the
URL
Behavior
property is set to
Dynamic
.
Public Class CH11BookServicesVB Inherits System.Web.Services.Protocols.SoapHttpClientProtocol Public Sub New( ) MyBase.NewDim urlSetting As String = _
System.Configuration.ConfigurationSettings.AppSettings( _
"VBExamples.ExampleBookServices.CH11BookServicesVB")
If (Not (urlSetting) Is Nothing) Then
Me.Url = String.Concat(urlSetting, "")
Else
Me.Url = _
"http://michaelk/ASPNetCookbook/VBWebServices/CH11BookServicesVB.asmx"
End If
End Sub ... End Class public class CH11BookServicesCS: System.Web.Services.Protocols.SoapHttpClientProtocol { public BookServices_CS( ) {string urlSetting =
System.Configuration.ConfigurationSettings.
AppSettings["CSExamples.ExampleBookServices.CH11BookServicesCS"];
if ((urlSetting != null))
{
this.Url = string.Concat(urlSetting, "");
}
else
{
this.Url =
"http://localhost/ASPNetCookbook/CSWebServices/CH11BookServicesCS.asmx";
}
} ... }
When you set the URL Behavior
property to
Dynamic
, Visual Studio .NET also adds an entry in
the appSettings
section your
web.config file. The key
is
set to the full namespace of the proxy class, and the
value
is set the same as the hardcoded value. By
just changing a property value, Visual Studio .NET has written all of
the code you need to support changing the URL of the web service
without having to change, recompile, and deploy code.
<appSettings><add key="VBExamples.ExampleBookServices.CH11BookServicesVB"
value="http://michaelk/ASPNetCookbook/VBWebServices/CH11BookServicesVB.asmx"/>
</appSettings>