Click here to Skip to main content
12,501,479 members (58,950 online)
Click here to Skip to main content
Add your own
alternative version

Stats

25.3K views
2 bookmarked
Posted

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

, 30 Aug 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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);
    }
}
}

License

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

Share

About the Author

BrianBissell
United States United States
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160919.1 | Last Updated 30 Aug 2011
Article Copyright 2011 by BrianBissell
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid