Function Overloading in Web Services

Function Overloading in Web Services

http://www.codeproject.com/KB/webservices/OverloadingInWebService.aspx

By dheerajindian | 9 Oct 2008
How to perform function overloading in Web Services
Is your email address OK? You are signed up for our newsletters but your email address is either unconfirmed, or has not been reconfirmed in a long time. Please click here to have a confirmation email sent so we can confirm your email address and start sending you newsletters again. Alternatively, you can update your subscriptions .

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.

Collapse
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 WebMethod s:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值