Click here to Skip to main content
Click here to Skip to main content

Function Overloading in Web Services

By , 8 Oct 2008
 

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

In the above example, we made one web service having class OverloadingInWebService. In the Web Service, we have added attribute Description, which is used to describe the web service purpose to client. In the above Web Service, we have two overloaded WebMethods:

public int Add(int a, int b) 

and

public float Add(float a, float b)

While running this Web service, it will show the following runtime error.

ViewError.JPG

Solution for the Above Error

The procedure to solve this problem is very easy. Start each method with a Web Method attribute. Add Description property to add a description of web method and MessageName property to change web method name.

[WebMethod(MessageName = "<name>", Description = "<description>")]
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); 
    } 
} 
}

Reason for the Above Error

The Overloading is supported by web services. But when WSDL (Web Service Description Language) is generated, it will not be able to make the difference between methods because WSDL does not deal on the base of parameters. By passing web methods –‘MessageName Property’, it changes the method name in WSDL. See the WSDL given below, the operation name is Add but the input method name is AddInt as well as output method name is also same (AddInt). The same will apply for Float also.

<wsdl:operation name="Add"> 
            <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Add 
            two integer Value</wsdl:documentation> 
            <wsdl:input name="AddInt" message="tns:AddIntSoapIn" /> 
            <wsdl:output name="AddInt" message="tns:AddIntSoapOut" /> 
</wsdl:operation> 
<wsdl:operation name="Add"> 
            <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Add     
            two Float Value</wsdl:documentation> 
            <wsdl:input name="AddFloat" message="tns:AddFloatSoapIn" /> 
            <wsdl:output name="AddFloat" message="tns:AddFloatSoapOut" /> 
</wsdl:operation>

History

  • 9th October, 2008: Initial post

License

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

About the Author

dheerajindian
Web Developer Hexaware Technologies
India India
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberSarvesh Kushwaha23 Dec '12 - 20:32 
Excellent
GeneralThanks a lotmemberPrashant Trambake20 Jun '12 - 0:29 
Really help me. Thanks a lot. Smile | :)
GeneralMy vote of 5memberBrianBissell30 Aug '11 - 3:29 
Thorough and well explained. Thank you for laying the problem out first and explaining the issue...many forget to put their answers in such great context!
 
Thanks!
GeneralMy vote of 5membermailtoravi10126 Feb '11 - 19:24 
Good article,explanation is simple and easy to understand.
 
Thanks for post.
Ravi
GeneralMy vote of 5memberRathika Krishnavelu27 Sep '10 - 22:34 
Really very nice. And more over short and sweet. Thanks for the Post.
GeneralCheap implementationmemberDonsw25 Jan '09 - 17:16 
Going against the standard.
GeneralRe: Cheap implementationmemberElCaito11 Feb '10 - 17:47 
What standard?
QuestionWhat about BP standard?memberMember 176454314 Oct '08 - 21:20 
[WebService(Namespace = "http://www.myNamespace.com/")]
//[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

 
It works only when you remove Basic Profile declaration as shown above.
 
Even though you get warning:
"This WebService does not comply to WS-I Basic Profile version 1.1 standard"
(translated from polish)
 
See: http://www.ws-i.org/Profiles/BasicProfile-1.1.html
 
4.5.3 Distinctive Operations
Operation name overloading in a wsdl:portType is disallowed by the Profile.
 
R2304 A wsdl:portType in a DESCRIPTION MUST have operations with distinct values for their name attributes.
 
Regards
Bartek
AnswerRe: What about BP standard?membereaston21 Feb '09 - 13:51 
Thank you for that insight Bartek... Good article, but missing that one crucial point to get overloading to work.
Generalnice articlememberKapil Thakur13 Oct '08 - 19:46 
hi dheeraj,
thanks for the article.
how will we consume the overloaded function in the calling web page.
 
Regards,
Kapil Thakur
(Where's there is Kapil , there is a way) - thakur.kapil@gmail.com

GeneralRe: nice articlememberdheerajindian13 Oct '08 - 19:57 
Hi Kapil,
After this consuming overloded function is such a eaxy. you need to
Kapil Thakur wrote:
consume it as normal Web Method in web services

 
.

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 9 Oct 2008
Article Copyright 2008 by dheerajindian
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid