KnowDotNet Visual Organizer

Dynamically Change URL to Web Service

Use Dynamic URL Behavior and App.Config

by Les Smith
Print this Article Discuss in Forums

Can I dynamically change the URL path to a Web Service by using an app.config file?  Yes, and the .NET Framework will do it for you if you use the Dynamic URL Behavior property of the Web Reference.

Once in a while you run into applications where developers have used a hard-coded IP address in setting up the Web Reference.  Then, the inevitable happens; the Web Service is moved to another server or the server is replaced, etc., and your application no longer works.  This article will describe the right way to set up the Web Reference so that this will never will happen to you.

First, if you don't already use app.config files, you should start immediately.  They are much easier to use than the old .INI files of bygone days, and they are the easy way to solve the problem at hand.  There are many articles on app.config files on this site, just search for "app.config."  Next, just follow the steps outlined below.

When you add your Web Reference, you will be prompted for the URL to the Web Service that you wish to call.  Make sure that you use the actual computer name plus the path to the web service.  Never use an IP address instead of the computer name. An example might be as follows.

Wrong Way

http://10.1.0.123/MyWebServices/MyWS.asmx


Right Way

http://websvr1/MyWebServices/MyWS.asmx


Once the Web Reference is built, click on the Web Reference in the Solution Explorer.  In the properties box, change the URL Behavior from Static to Dynamic.  If you do not already have an app.config file in your project, one will be created automatically and a key like the one shown below will be inserted into the appSettings element of if the app.config file.

<add key = "MyProject/MyWebServices/MyWS" value = "http://websvr1/MyWebServices/MyWS.asmx" />


Now, for the neat part.  You may be wondering what code you have to write to set the URL of the Web Service after you retrieve the value from the app.config file at run-time.  Forget it: when you set the URL Behavor property,
the code was generated for you automatically.  In addition to the Web Reference in the Solution Explorer, if you click the "show all files" button at the top of the explorer, you will see a "+" beside the Web Reference.  Click the plus to expand the files under it.  There you will see a proxy class file named Reference.vb.  Open it by double-clicking it and you wil see that it has a constructor like the one shown below.

   Public Sub New()
      
MyBase.New()
      
Dim urlSetting As String = _
         System.Configuration.ConfigurationSettings.AppSettings _
             ("
MyProject/MyWebServices/MyWS")
      
If (Not (urlSetting) Is Nothing) Then
         Me.Url = String.Concat(urlSetting, "")
      
Else
         Me.Url = "http://websvr1/MyWebServices/MyWS.asmx"
      
End If
   End Sub

The code shown above will dynamically set your URL for the Web Service from the app.config when the application is loaded.  Having done all of this, you will never have to recompile your application just because something changed with regard to the computer that the Web Service runs on.

By the way, I recently had to do this in over a dozen projects lately due to the Web References having been originally coded with IP addresses and the web servers were going to have new IP addresses.  What a chore!  Hope this little trick saves you some time and possibly some broken applications.

Ask a Question, or give your feedback on my articles or products by clicking on My Blog.

Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing