Method Overloading in Web Service


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

Up Next
    Ebook Download
    View all
    Learn
    View all