Can web methods be overloaded ?

  • Thread starter Thread starter john bailo
  • Start date Start date
john, yes you can but you need to specify the MessageName attribute though
as an alias for the WSDL. Here's an example.

<%@ WebService Language="C#" Class="Calculator" %>

using System;
using System.Web.Services;

public class Calculator : WebService {
// The MessageName property defaults to Add for this XML Web service
method.
[WebMethod]
public int Add(int i, int j) {
return i + j;
}
[WebMethod(MessageName="Add2")]
public int Add(int i, int j, int k) {
return i + j + k;
}
}
 
if I want to use 3 parameters,

when I call the method from a client,

do I use Add or Add2 as my method?

Or is an 'alias' just a 'cosmetic' name for the overloaded
Add function?

Greg Ewing said:
john, yes you can but you need to specify the MessageName attribute though
as an alias for the WSDL. Here's an example.

<%@ WebService Language="C#" Class="Calculator" %>

using System;
using System.Web.Services;

public class Calculator : WebService {
// The MessageName property defaults to Add for this XML Web service
method.
[WebMethod]
public int Add(int i, int j) {
return i + j;
}
[WebMethod(MessageName="Add2")]
public int Add(int i, int j, int k) {
return i + j + k;
}
}

--
Greg Ewing [MVP]
http://www.citidc.com


john bailo said:
Can a web method be overloaded?

To support varying numbers of input parameters?
 
john, the client will have to use Add2.

--
Greg Ewing [MVP]
http://www.citidc.com

john bailo said:
if I want to use 3 parameters,

when I call the method from a client,

do I use Add or Add2 as my method?

Or is an 'alias' just a 'cosmetic' name for the overloaded
Add function?

Greg Ewing said:
john, yes you can but you need to specify the MessageName attribute though
as an alias for the WSDL. Here's an example.

<%@ WebService Language="C#" Class="Calculator" %>

using System;
using System.Web.Services;

public class Calculator : WebService {
// The MessageName property defaults to Add for this XML Web service
method.
[WebMethod]
public int Add(int i, int j) {
return i + j;
}
[WebMethod(MessageName="Add2")]
public int Add(int i, int j, int k) {
return i + j + k;
}
}

--
Greg Ewing [MVP]
http://www.citidc.com


john bailo said:
Can a web method be overloaded?

To support varying numbers of input parameters?
 
Hi,

No Web Service methods can not be overloaded. If you want to expose an
overloaded class method, you will have to provide a alias using the
MessageName property of the WebMethodAttribute.

C# : [WebMethod( MessageName="GetCustomerByID" )]

VB.NET <WebMethod( MessageName := "GetCustomerByID)>

Hope this helps

Chris Taylor
 
Chris Taylor dribbled:
Hi,

No Web Service methods can not be overloaded. If you want to expose an

ok, so the benefits of overloading
are lost when calling a web method.

to me, that's a shame, because the ability
to send varying numbers of parameters and
parameter types to the same method would be
really valuable for a remote client, talking
to a web method.

if you think http post, there may be several
uses of the same function but with different
UI's that would want to use the method.
 
Burgess said:
Chris Taylor dribbled:




ok, so the benefits of overloading
are lost when calling a web method.

to me, that's a shame, because the ability
to send varying numbers of parameters and
parameter types to the same method would be
really valuable for a remote client, talking
to a web method.

if you think http post, there may be several
uses of the same function but with different
UI's that would want to use the method.
You can easily handle this by making the web method accept a XMLDocument
as input rather than explicit parameters. They you would have control
over the kind of stuff you can get into that web method and you would
need to overload methods.

Of course, this would mean that you now have to write code to
de-serialize XML document to a real object. But that is a *good* thing.
 
Chris,

I tried your VB.NET version but it does not work. Firstly, I think you
forgot the closing " after GetCustomerByID but I get the following error !


Attribute "WebMethodAttribute" cannot be applied to '.ctor' because the
attribute is not valid on this attribute type.


Any ideas ?


ThanX !

Bubba !



Chris Taylor said:
Hi,

No Web Service methods can not be overloaded. If you want to expose an
overloaded class method, you will have to provide a alias using the
MessageName property of the WebMethodAttribute.

C# : [WebMethod( MessageName="GetCustomerByID" )]

VB.NET <WebMethod( MessageName := "GetCustomerByID)>

Hope this helps

Chris Taylor

john bailo said:
Can a web method be overloaded?

To support varying numbers of input parameters?
 
Hi Bubba,

Thanks for posting to the newsgroup.

From the error message, it appears as though you're trying to create an
"overloaded" constructor for your web method. Is this really what you want
to do?

- bliz
--
Jim Blizzard, MCSD .NET
Community Developer Evangelist | http://www.microsoft.com/communities
Microsoft

Your Potential. Our Passion.

This posting is provided as is, without warranty, and confers no rights.

Bubba Thomas said:
Chris,

I tried your VB.NET version but it does not work. Firstly, I think you
forgot the closing " after GetCustomerByID but I get the following error !


Attribute "WebMethodAttribute" cannot be applied to '.ctor' because the
attribute is not valid on this attribute type.


Any ideas ?


ThanX !

Bubba !



Chris Taylor said:
Hi,

No Web Service methods can not be overloaded. If you want to expose an
overloaded class method, you will have to provide a alias using the
MessageName property of the WebMethodAttribute.

C# : [WebMethod( MessageName="GetCustomerByID" )]

VB.NET <WebMethod( MessageName := "GetCustomerByID)>

Hope this helps

Chris Taylor

john bailo said:
Can a web method be overloaded?

To support varying numbers of input parameters?
 
Back
Top