Click here to Skip to main content
15,909,566 members
Articles / Programming Languages / Visual Basic
Article

Configurable WebService Servers

Rate me:
Please Sign up or sign in to vote.
2.57/5 (7 votes)
24 Sep 20033 min read 64.6K   19   7
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.

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

To the following:

VB
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:

VB
Dim S1 As New localhost.Service1()

Changed to this:

VB
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:

VB
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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralSome related reading Pin
franjalova9-Dec-05 13:41
franjalova9-Dec-05 13:41 
General[Message Removed] Pin
nompel5-Oct-08 0:07
nompel5-Oct-08 0:07 
GeneralUnecessary Pin
bmichael25-Sep-03 16:23
bmichael25-Sep-03 16:23 
GeneralRe: Unecessary Pin
DudleyDoorite26-Sep-03 2:54
DudleyDoorite26-Sep-03 2:54 
Thank you for your reply. I did not now that these options was available. I had asked a question on the forums about this
sitiation but got no reply. While waiting I came across this method.

Again thank you for helping me out on this.

Thanks
Dudley


=================================
When I was in school, all I wanted was to get out into the real world.
Now that I'm in the real world, all I want is to go back to school.
GeneralRe: Unecessary Pin
waynea27-Nov-03 18:05
waynea27-Nov-03 18:05 
GeneralRe: Unecessary Pin
Anonymous28-Jan-04 9:37
Anonymous28-Jan-04 9:37 
GeneralRe: Unecessary Pin
The Chiz28-Jan-04 17:31
The Chiz28-Jan-04 17:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.