Click here to Skip to main content
Click here to Skip to main content

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.

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 Big Grin | :-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
      {
        [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:
    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;
      objProxy.Url=GetHWServiceURL();
      Response.Write(objProxy.HelloWorld());
    }
    public string GetHWServiceURL()
    {
      return 
        System.Configuration.ConfigurationSettings.AppSettings["HWServiceURL"];
    }
  • The web.config looks like this:
    <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

About the Author

Het2109
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
QuestionDynamic Webservice call from windows service Pinmemberpratap5575-Jul-09 20:07 
GeneralWebReference in DLL Pinmemberakumafire10-May-07 5:55 
GeneralDynamic Ref PinmemberSanjeevKumarG25-Mar-07 18:58 
GeneralAnother simple way PinmemberJay Dubal30-May-05 5:06 
GeneralRe: Another simple way PinmemberHet210930-May-05 16:40 
GeneralRe: Another simple way PinmemberVladCo19-Jun-05 14:51 
GeneralRe: Another simple way PinmemberDogu Tümerdem7-Jul-06 4:07 
I have a dll to access to my web services. When I create a deploy project for related exe file, dependent dll will automatically added to project. But when deploying the client the DLL's config file created(!) some where else ???
 
in such a case i hope it may be useful...

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 6 Jun 2005
Article Copyright 2005 by Het2109
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid