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 |
| http://websvr1/MyWebServices/MyWS.asmx |
| <add key = "MyProject/MyWebServices/MyWS" value = "http://websvr1/MyWebServices/MyWS.asmx" /> |
| 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 |
| Ask a Question, or give your feedback on my articles or products by clicking on My Blog. | ![]() |