Using the 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 the other Web Services. One big feature of Web Services is, that Web Services can communicate with heterogeneous applications because Web Services present the information in XML format. The following figure shows how Web Services can communicate with a heterogeneous application.

Web-Service

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 the 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.

Web-Service

A WSDL document contains information about the method that a Web Service supports and how it 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, and 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 web service 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 http://tempuri.org, 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 the WebMethod attribute, can not be called from the client side.

Creating A Simple ASP.Net Web Service


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":

Web-Service

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:

Web-Service

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 = "http://tempuri.org/")]  
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
    [System.ComponentModel.ToolboxItem(false)]  
    // 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  
    {  
   
        [WebMethod]  
        public int Add(int num1, int num2)  
        {  
            return num1 + num2;  
        }  
        [WebMethod]  
        public int Sub(int num1, int num2)  
        {  
            return num1 - num2;  
        }  
        [WebMethod]  
        public int Mult(int num1, int num2)  
        {  
            return num1 * num2;  
        }  
        [WebMethod]  
        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, Subtracting, Multiplying, and Dividing two numbers.

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.

Web-Service

Now if you click on the Service description then you will get the description of the service in XML you will see the WSDL

Web-Service

Creating a Windows Client

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

Web-Service

Click on the "Smart task panel" of the ComboBox then select Add Items -> Add, Subtract, Multiply, and Divide. Right-click on the References folder select "Add Service reference" then paste the WSDL file address in the Address Bar.

Web-Service

Click "OK". Call the Web Service on the Click Event of the Button Double-click on the "Calculate" Button, then enter the following code.

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.

Web-Service

How it works

When you click on the calculate button, the proxy object of the web service 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.

Summary

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.