Click here to Skip to main content
15,881,882 members
Articles / Web Development / IIS
Article

How to Configure Web Service Calls from Client Applications at Runtime

Rate me:
Please Sign up or sign in to vote.
3.27/5 (22 votes)
6 Jun 20054 min read 190K   1.2K   45   7
A simple and easy-to-understand project on how to configure Web Service calls from client applications at runtime.

Introduction

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:
    C#
    using System;
    using System.Web.Services;
    
    namespace HWWebService
    {
      public class HWClass : System.Web.Services.WebService
      {
        [WebMethod]
        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:
    C#
    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:
    C#
    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:
    C#
    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:

    C#
    private void btnWS_Click(object sender, System.EventArgs e)
    {
      localhost.HWClass objProxy=new localhost.HWClass;
      objProxy.Url=GetHWServiceURL();
      Response.Write(objProxy.HelloWorld());
    }
    C#
    public string GetHWServiceURL()
    {
      return 
        System.Configuration.ConfigurationSettings.AppSettings["HWServiceURL"];
    }
  • The web.config looks like this:
    XML
    <appSettings>
      <add key="HWServiceURL" 
              value="http://localhost/HWWebService/HWService.asmx" />
    </appSettings>

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

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

 
QuestionDynamic Webservice call from windows service Pin
pratap5575-Jul-09 20:07
pratap5575-Jul-09 20:07 
GeneralWebReference in DLL Pin
akumafire10-May-07 5:55
akumafire10-May-07 5:55 
GeneralDynamic Ref Pin
SanjeevKumarG25-Mar-07 18:58
SanjeevKumarG25-Mar-07 18:58 
GeneralAnother simple way Pin
Jay Dubal30-May-05 5:06
Jay Dubal30-May-05 5:06 
GeneralRe: Another simple way Pin
Het210930-May-05 16:40
Het210930-May-05 16:40 
GeneralRe: Another simple way Pin
VladCo19-Jun-05 14:51
VladCo19-Jun-05 14:51 
GeneralRe: Another simple way Pin
Dogu Tmerdem - removed7-Jul-06 4:07
Dogu Tmerdem - removed7-Jul-06 4: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.