WorldofASP.NET : ASP.NET Tutorial, Hosting, and Source Code

WorldofASP.NET >> General NET >> Applications

Creating and Consuming XML Web Services

Creating a Simple Web Method and Using it on a Client Application

Published Date : 04 May 2008

Author : Hans Candra
Language : C#
Platform : .NET
 
Technology : Visual Studio
Views : 88
Rating : (0 votes so far)
Email to a Friend | Print this Article | Add to Favourites | Report Error

Introduction

A Web service is defined as "a software system designed to support interoperable Machine to Machine interaction over a network." Web services are frequently just Web APIs that can be accessed over a network, such as the Internet, and executed on a remote system hosting the requested services.

Main

The Web service definition encompasses many different systems, but in common usage the term refers to clients and servers that communicate using XML messages that follow the SOAP standard. It is a prerequisite for automated client-side code generation in many .NET SOAP frameworks.

Here, you can learn about how to create and consume a simple XML Web services. Below figure shows how various components of the XML Web services infrastructure enable clients to locate and call methods on XML Web services.



Last time there is a uddi directory from Microsoft in which we can import all required Web Services and Methods but later it is closed from public probably concerning about security reason.

Creating XML Web Services

Open the Visual Studio .NET. Create new project. In the New Project dialog box, select C# Projects in the Project Types pane. In the Templates pane, select ASP.NET Web Service. In the Location box, type the name of the XML Web service Eg: XMLWebService.



After that in Solution explorer rename your .asmx file to MyService.asmx. To add functionality to the XML Web service, select the Solution Explorer tab, right-click the MyService.asmx file, and choose View Code. Now we are creating several Web methods. Web methods are independent functional units that perform certain predefined operations in an XML Web service. Example:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace XMLWebServices
{
    public class MyService : System.Web.Services.WebService
    {
        public MyService(){}
        [WebMethod] //this is the sytax to create web method
        public int AddNumbers(int num1, int num2)
        {
            return (num1 + num2);
        }
        [WebMethod]
        public int SubNumbers(int num1, int num2)
        {
            return (num1 - num2);
        }
        [WebMethod]
        public int MulNumbers(int num1, int num2)
        {
            return (num1 * num2);
        }
        [WebMethod]
        public int DivNumbers(int num1, int num2)
        {
            return (num1 / num2);
        }
    }
}

Consuming XML Web Services

Add a new item to the project and then choose web form. Rename it to calculation.aspx. For example you can make it simple like the one below.




using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace XMLWebServices
{
    public partial class calculation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
 
        protected void Button1_Click(object sender, EventArgs e)
        {
            MyService ServiceObj = new MyService();
            Label4.Text = ServiceObj.AddNumbers(Convert.ToInt32(TextBox1.Text), 
Convert.ToInt32(TextBox2.Text)).ToString();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            MyService ServiceObj = new MyService();
            Label4.Text = ServiceObj.SubNumbers(Convert.ToInt32(TextBox1.Text), 
Convert.ToInt32(TextBox2.Text)).ToString();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            MyService ServiceObj = new MyService();
            Label4.Text = ServiceObj.MulNumbers(Convert.ToInt32(TextBox1.Text), 
Convert.ToInt32(TextBox2.Text)).ToString();
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            MyService ServiceObj = new MyService();
            Label4.Text = ServiceObj.DivNumbers(Convert.ToInt32(TextBox1.Text), 
Convert.ToInt32(TextBox2.Text)).ToString();
        }
    }
}

After it build your solution. And run calculation.aspx. You already consumed an XML Web service by using a sample XML Web service, which contains four Web methods that allow you to perform simple arithmetic operations, such as addition, subtraction, multiplication, and division of two integers. All these Web methods take the two integers as parameters. Result of it shown below:

Conclusion

We can build XML Web services that the clients can consume by using the ASP.NET infrastructure, which is an integral part of the .NET Framework. Visual Studio .NET provides tools to build, deploy, and publish your XML Web services using ASP.NET. You will find anew way to performing remote method calls. You can build scalable & loosely coupled applications. In mor depth after this you can find more about XML Web services, it enable disparate applications to exchange messages using standard protocols such as HTTP, XML, XSD, SOAP, and Web Services Description Language (WSDL). The .NET Framework make it easy for you to make use of all this. 



        Other Related and Popular Articles :

        How to Generate Random Numbers in ASP.NET and .NET Framework
        Generating Random Number in ASP.NET and .NET Framework
        Creating Windows Services in .NET
        Basic example and tutorial about Windows Services in .NET

        Author Profile : Hans Candra

        Click here to view Author Profile


        How would you rate the quality of this content?
        Poor Excellent

        Comments

        Leave New Comments


        Article Content copyright by Hans Candra
        Everything else Copyright © by WorldofASP.NET 2008
         
        Announcements
        Earn Cash Now







        Legend : - Within 3 Days - Within 6 Days - Within 9 Days

        Home | Add Resources | Sponsored Listings | Advertise with Us | Contact Us | SiteMap | Link To Us | Contact Us
        © 2002-2008 Worldofasp.net ASP.NET Directory, Hosting and Tutorials | All rights reserved
        Our Partners : ASP.NET Web Hosting | Windows Web Hosting | FREE ASP.NET CMS | Phone Card | PHP Directory | Bangkok Hotels |Calling Card