Mar 12, 2014

How to Consuming a web service in Asp.net

http://mvkr.blogspot.in/2014/03/how-to-create-aspnet-web-services.html

Please go through the link before you proceed for this article


Step 1: Right click on WebServicesDemo solution in solution explorer and add new asp.net web application project and name it CalculatorWebApplication.



Step 2: Now we need to add a reference to web service. To achieve this 

a) Right click on References folder in the CalculatorWebApplication project and select Add Service Reference option

b) In the Address textbox of the Add Service Reference window, type the web service address and click GO button. In the namespace textbox type CalculatorService and click OK



Step 1: Right click on WebServicesDemo solution in solution explorer and add new asp.net web application project and name it CalculatorWebApplication.

Step 2: Now we need to add a reference to web service. To achieve this
a) Right click on References folder in the CalculatorWebApplication project and select Add Service Reference option
b) In the Address textbox of the Add Service Reference window, type the web service address and click GO button. In the namespace textbox type CalculatorService and click OK.  





 Step 3: Right click on CalculatorWebApplication project in solution explorer and add new webform.
Step 3: Copy and paste the following HTML <table style="font-family: Arial"> <tr>
    <td>
        <b>First Number</b>
    </td>
    <td>
        <asp:TextBox ID="txtFirstNumber" runat="server"></asp:TextBox>
    </td>
</tr> <tr>
    <td>
        <b>Second Number</b>
    </td>
    <td>
        <asp:TextBox ID="txtSecondNumber" runat="server"></asp:TextBox>
    </td>
</tr> <tr>
    <td>
        <b>Result</b>
    </td>
    <td>
        <asp:Label ID="lblResult" runat="server"></asp:Label>
    </td>
</tr> <tr>
    <td colspan="2">
        <asp:Button ID="btnAdd" runat="server" Text="Add"
        OnClick="btnAdd_Click" />
    </td>
</tr> </table>


Step 4: Copy and paste the following code in the code-behind file protected void btnAdd_Click(object sender, EventArgs e) {
    CalculatorService.CalculatorWebServicesSoapClient client =
        new CalculatorService.CalculatorWebServicesSoapClient();
    int result = client.Add(Convert.ToInt32(txtFirstNumber.Text),
        Convert.ToInt32(txtSecondNumber.Text));
    lblResult.Text = result.ToString();
}

In an interview the interviewer may ask you the following questions related to consuming a web service.  

1. What is WSDL and what is it's purpose
 2. How is a proxy class generated  
3. What is the use of a proxy class
4. What actually happens when a web service reference is added

The following 2 paragraphs should provide the answers for the above questions  

Visual studio generates a proxy class using the WSDL (Web Service Description Language) document of the web service. 
The WSDL document formally defines a web service. It contains 

 1. All the methods that are exposed by the web service
 2. The parameters and their types  
3. The return types of the methods

This information is then used by visual studio to create the proxy class. The client application calls the proxy class method. The proxy class will then serialize the parameters, prepares a SOAP request message and sends it to the web service. The web service executes the method and returns a SOAP response message to the proxy. The proxy class will then deserialize the SOAP response message and hands it the client application. We don't have to serialize or deserialize dot net CLR objects to and from SOAP format. The proxy class takes care of serialization and deserialization and makes the life of a developer much easier. 




O/P:





No comments:

Post a Comment