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 their platform independence. Web Services are also useful for developing a .NET application on both 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 the 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 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 service that will be in 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 are not mark with WebMethod attribute, they can not be called from client side.

Creating A Simple ASP.Net Web Service

1. Creating Web Service Project

Crete 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

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Services;


namespace SimpleCalcService


    /// <summary>

    /// Summary description for CalcService

    /// </summary>

    [WebService(Namespace = "")]

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]


    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

    // [System.Web.Script.Services.ScriptService]

    public class CalcService : System.Web.Services.WebService




        public int Add(int num1, int num2)


            return num1 + num2;



        public int Sub(int num1, int num2)


            return num1 - num2;



        public int Mult(int num1, int num2)


            return num1 * num2;



        public int Div(int num1, int num2)


            return num1 / num2;




The code above is pretty simple. We are just creating four web methods for Adding, Subtractig, 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 selet 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 Windows Client

Add a new C# Windows application. Then design the form as shown in the following figure.


Click on "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.

private void button1_Click(object sender, EventArgs e)


int num1, num2;

num1 = Convert.ToInt32(textBox1.Text);

num2 = Convert.ToInt32(textBox1.Text);

ServiceReference1.CalcServiceSoapClient proxy = new ServiceReference1.CalcServiceSoapClient();//Creating proxy object of Web Service

if (comboBox1.Text == "Add")


MessageBox.Show(proxy.Add(num1, num2).ToString());//Calling Add method of CalcWeb Service


else if (comboBox1.Text == "Sub")


MessageBox.Show(proxy.Sub(num1, num2).ToString());


else if (comboBox1.Text == "Mul")


MessageBox.Show(proxy.Mult(num1, num2).ToString());


else if (comboBox1.Text == "Div")


MessageBox.Show(proxy.Div(num1, num2).ToString());



Run your Windows application. Enter numbers in textBox1 and TextBox2  then select "Operation" from the ComboBox then clcik 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 web server, 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.