Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Configurable WebService Servers

0.00/5 (No votes)
24 Sep 2003 1  
Change WebService server locations at runtime.

Introduction

I have been creating a number of small applications that take a document and pass it to a web service to be processed. I developed the Web Service and applications on a development computer, and would copy all to the production computer when completed. This worked fine until I started to migrate the applications to a second computer (App Server) and leave the Web Service on the Production Computer (Process Server). I found I needed to change the applications' web reference from localhost to the location of the ProdServer (Process server). Again, this was fine until the (Process Server) crashed and was no longer useable. I moved the Web Service onto the App Server and again recompiled the applications to have the Web Reference of localhost.

I started thinking that it would have been much more efficient if I could have changed a configuration setting and have the applications point to a different server for its Web Services. If this was possible, I could have pointed the application to the test server and restarted the application. When the Production Server was corrected, change the configuration back, restart the application, and let it go. After doing a little research, I found that I could do this rather simply.

Solution

When you use the VS IDE to create a Reference to a Web Service, it auto generates a file called Reference.vb. In this file, I found that there is a reference to the server's URL for where the Web Service is located. I made a copy of this file and deleted all references to the current Web Service. I created a class file and pasted the code from the Reference.vb into my new class called DynamicWS. I changed every reference in my applications from localhost to DynamicWS.

Next, I made a small change to the DynamicWS.

        Public Sub New()
            MyBase.New
            Me.Url = "http://localhost/DynamicWS/DWS.asmx"
        End Sub

To the following:

        Public Sub New(ByVal ServerUrl As String)
            MyBase.New
            Me.Url = "http://" & ServerUrl & "/DynamicWS/DWS.asmx"
        End Sub

After modifying the DynamicWS, I made a simple change to the web service declaration:

Dim S1 As New localhost.Service1()

Changed to this:

Dim S1 As New DynWS.Service1("localhost")

Step-by-Step Proof of Concept

First, you will need two machines capable of hosting a Web Service. Next, on each machine, create a basic web service and uncomment the HelloWorld Web Service that is in the basic project. On Machine #1, change the return value for the HelloWorld to something like "Hello World From the Production Server". Do the same on your Machine #2, but change the return value to "Hello World From the Test Server". Test each web service to verify they are returning the values you want.

Next, create a simple application with a TextBox. This is where we will put our server location. Add a button to consume your Web Service. Create a Web Reference to your first machine, so the IDE will auto generate a Reference.vb file. Copy the contents of this file to a new class file in your project. Delete the Web Reference. Make the changes listed above to your new class, then under your Button1_Click procedure, add the following:

        Dim S1 As New DynWS.Service1(TextBox1.Text.Trim)
        Debug.WriteLine(S1.HelloWorld)

When you run your application, enter the server name or address to the computer that has your web service, and press the button. When you change the name to your second computer, you should get a separate message.

Possible Improvements

  • Create an add-in to create this new class without having to add a Web Reference, copy/paste, then delete Reference.
  • Add some error checking into the application to make sure your server has the correct Web Service during runtime.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here