Building ASP.NET Web Services

Introduction

XML (eXtensible Markup Language) came into existence after several years of HTTP/HTML revolution. If I brief few important features of XML, XML is a simple and easy-to-understand format for data markup. From this single, easy manner of data encoding, it wasn't too large a step to create a new application framework that combined HTTP and XML into Web service-and enabled developers to create distributed applications in ways that were impossible before. Defining a web service is a difficult task. But in brief we can define web service as application logic that is accessible using Internet standards. Or this is a secure and reliable service provided over the internet or intranet. The extension for asp.net web service file is .asmx.

WSDL (Web Services Description Language)

WSDL can be defined as a language (document) which describes a web service. Hence one more definition of the web service can be given as anything a Web Services Description Language (WSDL) document can describe. The Web Services Description Language tool (WSDL.exe) generates code for XML Web services and XML Web service clients from WSDL contract files, XSD schemas, and .discomap discovery documents. Open Visual studio.Net command prompt and type WSDL /? To get list of options available with this tool.

Below command generates .wsdl file for a web service located in specified URL.

Wsdl
http://localhost/MyServices/ArithmeticService.asmx?wsdl

Two major features in .NET are its ability to create XML Web Services Servers and XML Web Services Clients. There are also several related APIs (application programming interfaces) available for manipulating DISCO (Web Services Discovery Language) and WSDL (Web Services Description Language) documents.

The engine that drives most of these features is XML Serialization. This API, found within the System.Xml.Serialization namespace, provides the ability to read and write XML without a DOM (Document Object Model) class or an XML-based stream reader. Instead, classes can be mapped to schema-based XML using a default set of rules, plus a variety of optional attributes to customize this serialization.

DISCO (Universal Description, Discovery, and Integration): This is a platform independent framework which acts like dictionary of the web services and helps to locate web services on the internet. This also provides description for the web service that we are searching for.

Proxy

For XML Web service clients, the proxy class handles the work of mapping parameters to XML elements and then sending the SOAP message over the network (to and fro). By default, the proxy class uses SOAP over HTTP to communicate with the XML Web service. But this can be changed by using WSDL.exe to use either the HTTP-GET protocol or HTTP-POST protocol.

Let's create a Web Service and consume it by the client application. This service returns Maximum of the two numbers sent. 

Create a Service

  1. Open Visual studio.Net. Click on File->New->Project and Select Visual C# Projects. Select ASP.Net Web Service.
  2. Give name for service in Location box, say http://localhost/MyServices.
  3. This will open default web service file by name service1.asmx. Change the name to ArithmeticService.asmx.
  4. Add below code.

    ///////////////////////////////////////////////////////////////////////////////////////////
    using
    System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Web;
    using System.Web.Services;
    namespace MyServices
    {
    /// <summary>
    /// Summary description for MyServices.
    /// </summary>
    public class ArithmeticService : System.Web.Services.WebService
    {
    public ArithmeticService()
    {
    //CODEGEN: This call is required by the ASP.NET Web Services Designer
    InitializeComponent();
    }
    #region Component Designer generated code
    //Required by the Web Services Designer
    private IContainer components = null;
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    }
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if(disposing && components != null)
    {
    components.Dispose();
    }
    base.Dispose(disposing);
    }
    #endregion
    [WebMethod]
    public long ReturnMaxNum(long num1,long num2)
    {
    try
    {
    return Math.Max(num1,num2);
    }
    catch(Exception ex)
    {
    throw ex;
    }
    }
    }
    }
    ///////////////////////////////////////////////////////////////////////////////////////////
  5. Build and run the application. This will open service on internet explorer
  6. Click on the Service Description link to get WSDL for this service.
  7. Click on ReturnMaxNum link to show test page. 

Create a proxy

Open Visual Studio.Net command prompt and move to the service folder and type below command.

WSDL http://localhost/MyServices/ArithmeticService.asmx 

This will generate a class file with ArithmeticService.cs  name. This is a proxy class for the web service. Create an assembly for the proxy class.

Consume Service by Client application

Create Visual Studio .Net windows application  and reference proxy assembly created and call the service to get the result.


Similar Articles