Wednesday, 17 June 2015

consume a web service from a client application

ASP.Net Web Services – How to consume a web service from a client application?

In last day, we were discussing about creating ASP.Net Web Services. You can read that article hereIn this article, we will go over consuming a web service from a client application.
Let’s try to consume the web service which we have created in the previous article. Follow the below steps for this.
Step 1Right click on WebServicesDemo solution in Solution Explorer and add a new ASP.Net Web Application Project and name it as CalculatorWebApplication.
ASP.NetWebServices1
Step 2Now we need to add a reference to the web service. To achieve this, first right click on the References folder in the CalculatorWebApplication project and select Add Service Reference option.
ASP.NetWebServices2
Then in the Address textbox of the Add Service Reference window, type the web service address and click on GO button. In the Namespace textbox, type CalculatorService and clickOK.
ASP.NetWebServices3
Once we click OK, Visual Studio will create a Proxy Class based on the WSDL document. Under the Service References folder, we can see the Namespace we have provided which isCalculatorService.
ASP.NetWebServices4
If you want to look at the generated Proxy Class, first click on the Show All Files button on the top. Then you can see a file named Reference.cs. 
ASP.NetWebServices7
Open that file. You can see a class called CalculatorWebServiceSoapClient. This is theProxy Class. Inside this class, you can see an Add method which is very much similar to theAdd method in the CalculatorWebService.
ASP.NetWebServices8
Step 3Right click on CalculatorWebApplication project in Solution Explorer and add a new Web Form named WebForm1.aspx.
Step 4Copy and paste the following HTML into the Web Form.
<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>
ASP.NetWebServices5
Step 5: Copy and paste the following code to the button click event in the code behind file.
protected void btnAdd_Click(object sender, EventArgs e)
{
CalculatorService.CalculatorWebServiceSoapClient client =
               new CalculatorService.CalculatorWebServiceSoapClient();
int result = client.Add(Convert.ToInt32(txtFirstNumber.Text),
                Convert.ToInt32(txtSecondNumber.Text));
lblResult.Text = result.ToString();
}
ASP.NetWebServices6
Now right click on the WebForm1.aspx and select View in Browser. Give some First Number & Second Number, say 20 and 30 and click on Add button. You will get a result as 50 as expected.
ASP.NetWebServices9
Here if we look at the web application, we don’t have the logic of adding two numbers. The addition is actually done by the web service. The client application accesses the web service using HTTP protocol. Messages with web services are exchanged in SOAP format. TheSerialization which is converting .Net types to SOAP request messages andDeserialization which is converting SOAP response back into .Net types are done by Proxy Class which in our case is CalculatorWebServiceSoapClient class.

No comments:

Post a Comment