Click here to Skip to main content
15,936,709 members
Articles / Web Development / ASP.NET
Alternative
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
30 Aug 2011CPOL 36K   2   6
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: I personally agree with you, however I am sure you can also ... Pin
BrianBissell7-Sep-11 3:56
BrianBissell7-Sep-11 3:56 
GeneralRe: Sorry for asking this then: isn't it easier to give each Web... Pin
KenBonny7-Sep-11 3:39
KenBonny7-Sep-11 3:39 
GeneralReason for my vote of 5 Very well described. But when I want... Pin
KenBonny6-Sep-11 20:54
KenBonny6-Sep-11 20:54 
GeneralRe: Yes, that is absolutely correct. Without this additional mes... Pin
BrianBissell7-Sep-11 3:17
BrianBissell7-Sep-11 3:17 
GeneralMethod prototype change is fine. Is it mandatory to specify ... Pin
Pradip_Bobhate31-Aug-11 5:01
Pradip_Bobhate31-Aug-11 5:01 
GeneralRe: Yes, each method signature must be unique just as with a reg... Pin
BrianBissell6-Sep-11 5:07
BrianBissell6-Sep-11 5:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.