Click here to Skip to main content
6,595,444 members and growing! (19,920 online)
Email Password   helpLost your password?
Web Development » Web Services » General License: The Code Project Open License (CPOL)

Function Overloading in Web Services

By dheerajindian

How to perform function overloading in Web Services
C#, VB (VB 8.0, VB 9.0, VB 6), Javascript, CSS, HTML, XHTML, .NET (.NET 2.0, .NET 3.0), ASP.NET, Ajax
Posted:9 Oct 2008
Views:9,249
Bookmarked:10 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
9 votes for this article.
Popularity: 1.77 Rating: 1.86 out of 5
4 votes, 44.4%
1
2 votes, 22.2%
2
2 votes, 22.2%
3

4
1 vote, 11.1%
5

Introduction

The function overloading in Web Service is not as state forward as in class. While trying to overload member function, we make two or more methods with same name with different parameters. But this will not work in web services and will show runtime error because WSDL is not supported 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 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 above Web Service we have two overloaded WebMethods -public int Add(int a, int b) and public float Add(float a, float b). While runing this Web service it will show following runtime error.

ViewError.JPG

Solution for 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>")]

Description property to add a description of web method and MessageName property to change web method name.

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 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 is not deal on the base of parameters. By passing web methods –‘MessageName Property’ it change 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). 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>

License

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

About the Author

dheerajindian


Member

Occupation: Web Developer
Company: Hexaware Technologies
Location: India India

Other popular Web Services articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralCheap implementation PinmemberDonsw18:16 25 Jan '09  
GeneralWhat about BP standard? PinmemberMember 176454322:20 14 Oct '08  
GeneralRe: What about BP standard? Pinmembereaston14:51 21 Feb '09  
Generalnice article PinmemberKapil Thakur20:46 13 Oct '08  
GeneralRe: nice article Pinmemberdheerajindian20:57 13 Oct '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Oct 2008
Editor: Sean Ewington
Copyright 2008 by dheerajindian
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project