Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Gauge for SharePoint
Search :       Advanced Search »
Home » Web Services » Building ASP.NET Web Services

Building ASP.NET Web Services

This article shows you how to write an ASP.NET Web service using Visual Studio .NET.

Page Views : 2846
Downloads : 0
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Team Foundation Server Hosting
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Ramaprasad Upadhyaya
Ramaprasad Upadhyaya works for Wipro Technologies as Project Engineer. He has around 4 Years software development experience in Asp, VB, ASP.Net, VB.Net, C#, Microsoft BizTalk Server, Microsoft SQL Server 2000. He is MCSD certified. He is one of Microsoft Most Valuable Professional[MVP] in .NET.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.