A Web Service is an external interface provided by a Web site that can be called from other Web sites. For example, a financial company may make up to the minute stock quotes available via Web Service for those who do their trading with that company. This information could be read from a Web page and displayed, or read from a stand-alone application on a customer's desktop computer.


 


Client Communicate through web service:


 


SOAP over HTTP SOAP over HTTP




XML XML


 


Location 1                                                                                                        Location 2


 


Soap calls are remote function calls that invoke methods executions on Web Service components at Location 2. The output is render as XML and pass back to the Location 1 where the user wants to access the Web Service.


At Location 1, the user need to consume some of the functions of the web service that is available on the internet, to make the remote function calls (RFC). All the processing can't be done on the remote machine, therefore, we have to create the proxy of the web service from where we can process our data. This whole process is also called as marshalling of data.


Due to security threats, we create the proxy of the server object here it is web service. That means we are creating a " proxy object " to act on behalf of the original Web service. The proxy object will have all the public available data interfaces as the original Web service.


 


Consuming a Web Service



The following are the steps to call/consume a webservice.


 


Step 1: Create a Web Service project and run it so that no compilation error exists.


Step 2: Create new Web Site that will consume this web site that is there in your system using the VS.Net. Create New Web Site with the name: Web Site Name that will consume web service: WebService_Consume.


Step 3: Once you are done with the creation of the new site that will consume the web Service. Let example the web service run as this URL: http://localhost:1508/WebService_New/MyWebService.asmx


 


 




 


Step 4: Right click on the solution, Click on “Add Web Reference”. This will open up a new pop-up window where you can specify the URL name of your web service .


 




 


Here we specify the web service URL and if you look at the right panel there is Web Reference Name: localhost [This the default name that is there].


 


Step5: We specify “localhost_myWebService” name for the web reference. This will include 3 files under this folder.



  • MyWebService.disco

  • MyWebService.discomap

  • MyWebService.wsdl


 


Step6: Look Inside the contents of the “disco” file


 


<?xml version="1.0" encoding="utf-8" ?>


<discovery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/disco/">


<contractRef ref="http://localhost:1508/WebService_New/MyWebService.asmx?wsdl" docRef="http://localhost:1508/WebService_New/MyWebService.asmx" xmlns="http://schemas.xmlsoap.org/disco/scl/" />


<soap address="http://localhost:1508/WebService_New/MyWebService.asmx" xmlns:q1="http://tempuri.org/" binding="q1:ServiceSoap" xmlns="http://schemas.xmlsoap.org/disco/soap/" />


<soap address="http://localhost:1508/WebService_New/MyWebService.asmx" xmlns:q2="http://tempuri.org/" binding="q2:ServiceSoap12" xmlns="http://schemas.xmlsoap.org/disco/soap/" />


</discovery>


 


Step7: Look Inside the contents of the “discomap” file


 


<?xml version="1.0" encoding="utf-8" ?>


<DiscoveryClientResultsFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">


<Results>


<DiscoveryClientResult referenceType="System.Web.Services.Discovery.DiscoveryDocumentReference" url="http://localhost:1508/WebService_New/MyWebService.asmx?disco" filename="MyWebService.disco" />


<DiscoveryClientResult referenceType="System.Web.Services.Discovery.ContractReference" url="http://localhost:1508/WebService_New/MyWebService.asmx?wsdl" filename="MyWebService.wsdl" />


</Results>


</DiscoveryClientResultsFile>


 


 


Example: Demonstrate the Consumption of the Web Service in the Web Site


 


 


using System;


using System.Data;


using System.Configuration;


using System.Web;


using System.Web.Security;


using System.Web.UI;


using System.Web.UI.WebControls;


using System.Web.UI.WebControls.WebParts;


using System.Web.UI.HtmlControls;


 


public partial class _Default : System.Web.UI. Page


{


  protected void Page_Load( object sender, EventArgs e)


    {


    }


  protected void Button1_Click( object sender, EventArgs e)


    { //Consuming the WebServices through Add Web Reference


    //Fetching the Value from the Web Services


        localhost_myWebService. Service objServiceConsume = new localhost_myWebService. Service ();


        txtName.Text = objServiceConsume.getEmployeeName();


        txtAge.Text = objServiceConsume.getEmployeeAge();


    }


}


 


Output:


 




 


Click on the Fetcher button: This will fetch the value from the Web Service.


 




 


 


 

                    

Copyright © 2012 VisualBuilder. All rights reserved