SIGN UP MEMBER LOGIN:    
ARTICLE

Method Overloading in Web Service

Posted by Alok Pandey Articles | Web Services in C# November 22, 2011
This article demonstrates method overloading in a web service with a simple example.
Reader Level:

Introduction:

Here I am creating a web service to describe method overloading in a web service. I am creating two functions with the same name: add. First add function has two integer parameters and second add method has three. Now we create a web services. Create a web service application and write the following code.

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.Services;
 

namespace
WebService1
{
    /// <summary>
    /// Summary description for Service1
    /// </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 Service1 : System.Web.Services.WebService
    {
 
        [WebMethod]
        public int add(int a,int b)
        {
            return a+b;
        }
        [WebMethod]
        public int add(int a, int b, int c)
        {
            return a + b + c;
        }
     }
}

Now run this service. You will note that it will show an error message, like the below figure.

method overloading in web service
method overloading in web service

It will generate an error because the web service does not support method overloading directly. We have to add the MessageName property to make difference between methods and change
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] to        [WebServiceBinding(ConformsTo = WsiProfiles.None)]. So modify the code with adding the MessageName property as written below.

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.Services;
 

namespace
WebService1
{
    ///
<summary>
    ///
Summary description for Service1
    ///
</summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.None)]
    [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 Service1 : System.Web.Services.
WebService
    {

        [WebMethod(MessageName="twoparameter",Description="addition of two integers")]
        public int add(int a,int b)
        {
            return (a + b);
        }
        [WebMethod(MessageName = "threeparameter", Description = "addition of three integers")]
        public int add(int a, int b, int c)
        {
            return (a + b + c);
        }
     }
}

Run the service.

Output:

 method overloading in web service

Now click at first add method. Give the value for valiables and invoke the method. It will show the result. Look at below figure.

method overloading in web service

After invoking the method.

Output:
 
method overloading in web service

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • 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.
    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. Visit DynamicPDF here
Nevron Gauge for SharePoint
Become a Sponsor