65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

Aug 30, 2011

CPOL
viewsIcon

36652

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.
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:
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);
    }
}
}