Calling a web service into another web service application


Introduction:

Generally we consume web services in web application, but there may be a need to call a web service in another web service application. As we know, a web application can consume more than one web service (here). In this article I am describing a simple way of calling a web service application in another web service application. A web service application can also call another web service. Here I will create two simple web services and will call one from another. 

At first we create a web service application. Follow the given steps.  

  • Go to Visual Studio 2010  and create a New Project
  • Select an ASP.NET Web Service Application
  • Give it a name and click ok button
Replace the code by the code given below:

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.Services;
 

namespace
firstservice
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string show()
        {
            return "Method from first web service";
        }
    }
}

Run this service application. Now create another ASP.NET Web Service Application (As above) and follow the given steps.

  • Go to Solution Explorer and right click at your project.

    calling web service into another web service
     
  • Select Add Service Reference. A new window will be open.

    calling web service into another web service
     
  • Click at "Advance" button ( Given in Above figure.). A new window will open.

    calling web service into another web service
     
  • Click at "Add Web Reference" button.

    calling web service into another web service

  • Copy the URL of first (Which has created at first) web service and paste it at the URL. Look at below screen - shot.

    web service article
     
  • Click the Go button.

    web service article
     
  • You can set Web reference name. the default name is "localhost". Click Add reference. 

Now a web reference has added into your web service application. Now replace the code with the following code.

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.Services;
 

namespace
secondwebservice
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
 
        [WebMethod(Description="Method of running web service")]
        public string show()
        {
            return "Method from second web service";
        }
        [WebMethod(Description="Method of called web service")]
        public string display()
        {
            localhost.Service1 obj = new localhost.Service1();
            return obj.show();
        }
    }
}
 
Run the service.

Output:

img777.gif

Click at display ( method ) to go to test page and click the "invoke" button.

web service article

Repeat the same for calling "show" method.

web service article


Similar Articles