Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

How to overload a web method in ASP.NET webservice...?

5.00/5 (1 vote)
30 Aug 2011CPOL 36.6K  
This provides a much better overview of the problem/solution:http://www.codeproject.com/KB/webservices/OverloadingInWebService.aspxIntroductionThe function overloading in Web Service is not as straightforward as in class. While trying to overload member function, we make two or more...
This provides a much better overview of the problem/solution:

http://www.codeproject.com/KB/webservices/OverloadingInWebService.aspx

Introduction

The function overloading in Web Service is not as straightforward as in class. While trying to overload member function, we make two or more methods with the same name with different parameters. But this will not work in web services and will show runtime error because WSDL is not supported by the same method name.

Overloading Web Services

While trying to overload Web Methods in Web Services and after doing the build, it will work successfully. But when we try to run or consume, it will show an error message. We can show this using an example.

C#
namespace TestOverloadingWebService
{ 
    [WebService(Namespace = "http://tempuri.org/", Description=" <b> Function 
    overloading in Web Services </b>")] 
public class OverloadingInWebService : System.Web.Services.WebService 
{ 
    [WebMethod()] 
    public int Add(int a, int b) 
    { 
        return (a + b); 
    } 
    [WebMethod()] 
    public float Add(float a, float b) 
    { 
        return (a + b); 
    } 
} 
}


Solution:

C#
namespace TestOverloadingWebService
{
    [WebService(Namespace = "http://tempuri.org/", Description=" <b> Function
    overloading in Web Services </b>")]
public class OverloadingInWebService : System.Web.Services.WebService
{
    [WebMethod(MessageName = "AddInt", Description = "Add two integer
        Value", EnableSession = true)]
    public int Add(int a, int b)
    {
        return (a + b);
    }
    [WebMethod(MessageName = "AddFloat", Description = "Add two Float
        Value", EnableSession = true)]
    public float Add(float a, float b)
    {
        return (a + b);
    }
}
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)