Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

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

, 30 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
In last day, we were discussing about creating ASP.Net Web Services. You can read that article here. In 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 be

In last day, we were discussing about creating ASP.Net Web Services. You can read that article here. In 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 1: Right click on WebServicesDemo solution in Solution Explorer and add a new ASP.Net Web Application Project and name it as CalculatorWebApplication.

ASP.NetWebServices1

Step 2: Now 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 click OK.

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 is CalculatorService.

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 the Proxy Class. Inside this class, you can see an Add method which is very much similar to the Add method in the CalculatorWebService.

ASP.NetWebServices8

Step 3: Right click on CalculatorWebApplication project in Solution Explorer and add a new Web Form named WebForm1.aspx.

Step 4: Copy and paste the following HTML into the Web Form.

<table style=&rdquo;font-family: Arial&rdquo;>
<tr>
    <td>
        <b>First Number</b>
    </td>
    <td>
        <asp:TextBox ID=&rdquo;txtFirstNumber&rdquo; runat="&rdquo;server&rdquo;"></asp:TextBox>
    </td>
</tr>
<tr>
    <td>
        <b>Second Number</b>
    </td>
    <td>
        <asp:TextBox ID=&rdquo;txtSecondNumber&rdquo; runat="&rdquo;server&rdquo;"></asp:TextBox>
    </td>
</tr>
<tr>
    <td>
        <b>Result</b>
    </td>
    <td>
        <asp:Label ID=&rdquo;lblResult&rdquo; runat="&rdquo;server&rdquo;"></asp:Label>
    </td>
</tr>
<tr>
    <td colspan=&rdquo;2&Prime;>
        <asp:Button ID=&rdquo;btnAdd&rdquo; runat="&rdquo;server&rdquo;" Text=&rdquo;Add&rdquo; 
        OnClick=&rdquo;btnAdd_Click&rdquo; />
    </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. The Serialization which is converting .NET types to SOAP request messages and Deserialization which is converting SOAP response back into .Net types are done by Proxy Class which in our case is CalculatorWebServiceSoapClient class.

Reference: Arun Ramachandran (http://BestTEchnologyBlog.Com)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Arun Ramachandran India
Software Developer
India India
Arun Ramachandran is a Software Engineer having hands on experience in different Microsoft Technologies who is presently working in Experion Technologies, India. He has written over 95 articles on the subject on his blog at http://BestTEchnologyBlog.com. Along with 3 years of hands on experience he holds a Master of Computer Applications degree from Cochin University of Science & Technology (CUSAT).

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141030.1 | Last Updated 30 Jul 2014
Article Copyright 2014 by Arun Ramachandran India
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid