Using Web Services in ASP.Net

Web Services

The topics of this article are:
  • Overview of Web Services
  • How to create Web Service with ASP.Net
  • How to use a Web Service from windows Form client

What is Web Services?

Web Services are server-side programs that listen for messages from client applications and return specific information.
This information may come from the Web Service itself, from other components in the same domain, or from the other Web Services.
One big feature of Web Services is, Web Services are able to communicate with heterogeneous applications because Web Services present the information in XML format.
The following figure shows how Web Services are able to communicate with a heterogeneous application.

Web Service Architecture

Web Services can use the SOAP protocol, which is a standard defined by many companies. A big advantage of a Web Service is its platform independence. Web Services are also useful for developing a .NET application on both the client and server-side. The advantage here is that client and the server can emerge independently. A service description is created with the Web Service Description Language (WSDL). WSDL is the XML based description of a Web Service.
A WSDL document contains information about the method that a Web Service supports and how they can be called.

What is UDDI?

UDDI is an XML-based standard for describing, publishing, and finding Web Services.
  • UDDI stands for Universal Description, Discovery, and Integration.
  • UDDI is a specification for a distributed registry of Web Services.
  • UDDI is a platform-independent, open framework.
  • UDDI can communicate via SOAP, CORBA, Java RMI Protocol.
  • UDDI uses WSDL to describe interfaces to Web Services.
  • UDDI is seen with SOAP and WSDL as one of the three foundation standards of Web Services.
  • UDDI is an open industry initiative enabling businesses to discover each other and define how they interact over the Internet.
Creating Web Service
To implement the Web Service you can derive the Web Service class from "System.Web.Services.WebService".
Web Service attribute
The subclass of a webservice should be marked with the webService attribute. The WebService attribute has the following properties:
  • Description: A description of the service that will be in the WSDL document.
  • Name: Gets or Sets the name of the Web Service.
  • Namespace: Gets or sets XML namespace for Web Service. The default value is, which is ok for testing, but before you make a service public you should change the namespace.
WebMethod Attribute: All the methods available from the Web Services must be marked with the WebMethod attribute.
The method that is not marked with WebMethod attribute, they can not be called from the client-side.

Creating A Simple ASP.Net Web Service

1. Creating a Web Service Project
Create a new Web Service project by selecting "File" -> "New" -> "Project..." and choose the "ASP.Net Empty Web Application" template. Give the name to the project "SimpleCalcService":
2. Add New Item
Right-click on the project  then select "Add New Item" -> WebService then provide the name "CalcService.asmx" to the file as in the following:
3. Write Down the Following code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Services;  
  7. namespace SimpleCalcService  
  8. {  
  9.     /// <summary>  
  10.     /// Summary description for CalcService  
  11.     /// </summary>  
  12.     [WebService(Namespace = "")]  
  13.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  14.     [System.ComponentModel.ToolboxItem(false)]  
  15.     // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.  
  16.     // [System.Web.Script.Services.ScriptService]  
  17.     public class CalcService : System.Web.Services.WebService  
  18.     {  
  20.         [WebMethod]  
  21.         public int Add(int num1, int num2)  
  22.         {  
  23.             return num1 + num2;  
  24.         }  
  25.         [WebMethod]  
  26.         public int Sub(int num1, int num2)  
  27.         {  
  28.             return num1 - num2;  
  29.         }  
  30.         [WebMethod]  
  31.         public int Mult(int num1, int num2)  
  32.         {  
  33.             return num1 * num2;  
  34.         }  
  35.         [WebMethod]  
  36.         public int Div(int num1, int num2)  
  37.         {  
  38.             return num1 / num2;  
  39.         }  
  40.     }  

The code above is pretty simple. We are just creating four web methods for Adding, Subtracting, Multiplying, and Dividing two numbers.
4. Testing The Web Service
Now you can test your service. Right-click on the CalcService.asmx file from Solution Explorer then select View in Browser, you will see your service is running as shown in the following figure.
Now if you click on the Service description then you will get the description about the service in XML, basically, you will see the WSDL
5. Creating a Windows Client
Add a new C# Windows application. Then design the form as shown in the following figure.
Click on the "Smart task panel" of the ComboBox then select Add Items -> Add, Subtract, Multiply and Divide.
Right-click on the References folder then select "Add Service reference" then
paste the WSDL file address in the Address Bar.
Click "Ok".
6. Call the Web Service on the Click Event of the Button
Double-click on the "Calculate" Button, then enter the following code.
  1. private void button1_Click(object sender, EventArgs e) {  
  2.     int num1, num2;  
  3.     num1 = Convert.ToInt32(textBox1.Text);  
  4.     num2 = Convert.ToInt32(textBox1.Text);  
  5.     ServiceReference1.CalcServiceSoapClient proxy = new ServiceReference1.CalcServiceSoapClient(); //Creating proxy object of Web Service  
  6.     if (comboBox1.Text == "Add") {  
  7.         MessageBox.Show(proxy.Add(num1, num2).ToString()); //Calling Add method of CalcWeb Service  
  8.     } else if (comboBox1.Text == "Sub") {  
  9.         MessageBox.Show(proxy.Sub(num1, num2).ToString());  
  10.     } else if (comboBox1.Text == "Mul") {  
  11.         MessageBox.Show(proxy.Mult(num1, num2).ToString());  
  12.     } else if (comboBox1.Text == "Div") {  
  13.         MessageBox.Show(proxy.Div(num1, num2).ToString());  
  14.     }  

Run your Windows application. Enter numbers in textBox1 and TextBox2  then select "Operation" from the ComboBox then click on the "Calculate" button. You will see the output as shown in the following figure.
How it works
When you clicked on the calculate button, the proxy object of the webservice will be created. Through that proxy object,
your application will communicate with the Calc Web Service. All calculations will be done on the webserver, the results will be returned to the client Application.


In this article, you saw how easy it is to create a Web Service in ASP.Net and how to call and bind that service to the client application.