Click here to Skip to main content
13,248,107 members (35,711 online)
Click here to Skip to main content
Add your own
alternative version


45 bookmarked
Posted 29 May 2005

How to Configure Web Service Calls from Client Applications at Runtime

, 6 Jun 2005
Rate this:
Please Sign up or sign in to vote.
A simple and easy-to-understand project on how to configure Web Service calls from client applications at runtime.


In one of my projects, I had to develop a web service and deliver the setup to the client. I faced problems while changing the Target Web Service as the client will install it anywhere in his machine with any name. With this article, I am trying to make it easy to understand for a person who is beginner to Web Services (just like me :-D), how to change the Target Web Service.

One of the most simplest ways of adding a web service to a client application is by adding a web reference to the web service by specifying the URL of the .asmx file. This generates the required proxy object, that's what VS.NET takes care of. However, it may happen that after adding the web reference, the web service is moved to some other location. In such cases, the most easy way is to recreate the proxy object. But what if the same thing happens after you deploy your web service client. It would be nice to allow a configurable URL so that even if the original web service is moved, your client applications need not be recompiled. In this article, we will see how to do just that.

I will create two web services here, one of which will have direct call and another web service will look into the web.config file for the reference.

Create web service

For our example, we will develop a simple web service that has only one method. The following steps will show you how to proceed.

  • Create a new C# Web Service project in VS.NET.
  • Open the default .asmx file and add the following code to it:
    using System;
    using System.Web.Services;
    namespace HWWebService
      public class HWClass : System.Web.Services.WebService
        public string HelloWorld()
            return "Hello Application, from Web Service";
  • As shown above, this web service class (HWClass) contains a single method called HelloWorld() that returns a string.
  • Add another .asmx file to the project.
  • Open the file and modify it as shown below:
    using System;
    using System.Web.Services;
    namespace HWWebService
      public class AnotherService : System.Web.Services.WebService
        [WebMethod]public string AnotherHelloWorld()
            return "Hello World, from Another Service";
  • This class is similar to the previous one but its name is AnotherService. Also, it returns a different string from AnotherHelloWorld() method so that you can identify the method call.
  • Now that we have both the web services ready, compile the project.

Creating the web service client

Let us build a simple web client for our web service.

  • Create a new ASP.NET web application in VS.NET.
  • The application will have a default web form. Before writing any code, we need to add a web reference to our web service. Right click on the References node and select Add web reference. Follow the same procedure as you would have while developing normal web services. Adding a web reference will generate code for the proxy web service object.
  • Place two text boxes. Name them as txtReturnWS and txtAnotherWS.
  • Place two buttons on the web form and name them tbnWS and btnAnotherWS. Add the following code in the Click event of the button:
    private void btnWS_Click(object sender, System.EventArgs e)
      HelloWorld.HWServiceClass objProxy = new HelloWorld.HWServiceClass();
      objProxy.Url = GetHWServiceURL();
      txtReturnWS.Text = objProxy.HelloWorld();
  • The above code shows how you will normally call a web service. The web reference contains information about the location of the web service.
  • If you move the .asmx file after you deploy this client, it is bound to get an error. To avoid such a situation, modify the above code as shown below:
    private void btnAnotherWS_Click(object sender, System.EventArgs e)
      AnotherWorld.AnotherService objProxy = new AnotherWorld.AnotherService();
      txtAnotherWS.Text = objProxy.AnotherHelloWorld();
  • In above code, we have explicitly set the Url property of the objProxy class to the required .asmx file.
  • You can store this URL in the <appSettings> section of the web.config file and retrieve it at run time. Now, even if you move your web service, all you need to do is change its URL in the web.config.
  • You can add the URL of the webservice in the web.config file in two ways:
    1. directly by adding the required tag to the web.config file and
    2. by right-clicking the service used in the application, set its property to dynamic. This will automatically add the tag required for the service, you can change the name of the service to "HWServiceURL".

    The following code shows this:

    private void btnWS_Click(object sender, System.EventArgs e)
      localhost.HWClass objProxy=new localhost.HWClass;
    public string GetHWServiceURL()
  • The web.config looks like this:
      <add key="HWServiceURL" 
              value="http://localhost/HWWebService/HWService.asmx" />

Note that in order for the above code to work correctly, both web services should have exactly same web method signatures.

Hope it will be useful to the team!

Happy dotnetting!

Het Waghela

Het Waghela, Blog | Het Waghela DotNet Questions Link | Het Waghela Resume | More Links


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


About the Author

Web Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionDynamic Webservice call from windows service Pin
pratap5575-Jul-09 21:07
memberpratap5575-Jul-09 21:07 

I am going through your article . I am developing a windows service , that will call webservice for mail/SMS integration. I would like to develop the windows service in such a way that If user puts details of webservice into a configuration xml file the application will automatically call the webservice for mail/sms. Could you plz help me in this regard. how to proceed??

GeneralWebReference in DLL Pin
akumafire10-May-07 6:55
memberakumafire10-May-07 6:55 
GeneralDynamic Ref Pin
SanjeevKumarG25-Mar-07 19:58
memberSanjeevKumarG25-Mar-07 19:58 
GeneralAnother simple way Pin
Jay Dubal30-May-05 6:06
memberJay Dubal30-May-05 6:06 
GeneralRe: Another simple way Pin
Het210930-May-05 17:40
memberHet210930-May-05 17:40 
GeneralRe: Another simple way Pin
VladCo19-Jun-05 15:51
memberVladCo19-Jun-05 15:51 
GeneralRe: Another simple way Pin
Dogu Tümerdem7-Jul-06 5:07
memberDogu Tümerdem7-Jul-06 5:07 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 6 Jun 2005
Article Copyright 2005 by Het2109
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid