Web Services with ASP .NET- Part-II
This is second part of the tutorial ‘Web Services with ASP.NET’. Please go through Web Services with ASP.NET–Part-I before you continue further.
Consuming a XML Web Service
Exposing data and logic as SOAP to disparate systems across the world becomes simple with the help of ASP.NET. But users are not limited to consuming XML Web services only into ASP.NET application. Web Services can be consumed in Windows forms, mobile applications, databases and more. But here we show the example of how to consume a web service from a .net based web application. Again we are describing the whole process step by step.
1. Create a new Web site by selecting File->New->Web Site from the IDE. When the New Web Site dialog opens , select the ASP.NET Web Site and name it ‘BindWebService’ as shown in Figure-9:

Figure-9: Creating Web Site ‘BindWebService’
2. To consume the web service ‘TestService’ that we have already created right click on the project in the solution explorer of ‘BindWebService’ and select ‘Add Reference’ as shown in Figure-10:

Figure-10: ‘Add Web Reference’ in the project
3. This pulls up the Add Web Reference dialog box as shown in Figure-11

Figure-11: ‘Add Web Reference’ Dialog Box
This dialog box enables users to point to a particular .asmx file to make a reference.
4. Now write down the URL of our created web service and click on ‘GO’. It will automatically find out the proper web service from localhost (as our test web service is in local machine).Now give the Web reference name as ‘MyService’ and then click on ‘Add Reference’ (Figure-12).When any third party wants to use our service he/she needs to mention the proper URL to fetch it (i.e http://abc.com/TestService/Service.asmx where abc.com is the server in which the web service is deployed for hosting)

Figure-12: Adding the proper web reference and rename it accordingly
5. Clicking the ‘Add Reference’ button causes Visual Studio to make an actual reference to the web service and we will find some additional files (web service’s WSDL file) as shown in Figure-13.
The application’s web.config file also contains the reference to the web service in its section.

Figure-13: Web service reference and additional files within solution explorer
6. Now we will show how to utilize web methods in our ‘BindWebService’ application. In the ‘Default.aspx’ page add the code as shown in Figure-14:

Figure-14: Source code for Default.aspx page
Here we take two text boxes in which user will enter two integers and after pressing the ‘ADD’ button the result (called through the web method) will be displayed at the ‘lblResult’ label.
7. Now from the design window double click on the ‘ADD’ button and this will give us the option to write code in the code behind page for the click event of ‘ADD’. The code is given within Figure-15:

Figure-15: Code for Click event of ‘btnAdd’
Here first we check the text boxes are blank or not. If any of them is blank the system will through an error message. Otherwise when the end user clicks the ‘ADD’ button after providing two integers , the application sends a SOAP request to the remote XML web service with the two input values as parameters and gets back a SOAP response containing the result (return value of ‘add’ web method). Proper parsing is required as we are converting string to integer (for arguments) and integer to string (for result).
8. So our application is ready for testing. Press ‘F5’ to run the application. Give two integers as input in the text boxes and press ‘ADD’ which will give you the result. Here we test the application for the values 10 and 15 and get the result 25 (shown in Figure-16)

Figure-16: Desired Output
We do not write code for addition in the code behind page and thus the web method ‘add’ for our previously build web service provide the proper result. Hence we can conclude that our application and the web service is working properly.
Summary
Web Services are an integral part of .Net platform and Visual Studio makes building and consuming web service very easy. This tutorial provides you a simple idea of what is web service and how to build it and access it from .Net based web application. This tutorial focuses on the basic functionalities of web service so that the beginners clearly understand the whole procedure.









Hi Jack. Thanks for feedback. We have tried to re size the image. Hope this will be helpful.
Hi Arunava,
Figure 14 and 15 are two small to read!
Showing the code in text or making the screen-shots bigger will surely help.
Great article for novices.
regards,
Jack.