Groups | Blog | Home
all groups > c# > october 2003 >

c# : Can web methods be overloaded ?



Greg Ewing [MVP]
10/14/2003 7:42:12 PM
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


[quoted text, click to view]

Greg Ewing [MVP]
10/14/2003 7:55:46 PM
john, the client will have to use Add2.

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

[quoted text, click to view]

john bailo
10/14/2003 11:27:07 PM

Can a web method be overloaded?

To support varying numbers of input parameters?


john bailo
10/14/2003 11:49:12 PM
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?

[quoted text, click to view]

Burgess Meredith
10/15/2003 1:47:23 AM
Greg Ewing [MVP] dribbled:

[quoted text, click to view]

Is that 'overloaded' then?

Isn't that just the same as two methods?


Burgess Meredith
10/15/2003 1:49:12 AM
Chris Taylor dribbled:

[quoted text, click to view]

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.

Chris Taylor
10/15/2003 3:02:22 AM
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

[quoted text, click to view]

Greg Ewing [MVP]
10/15/2003 8:08:48 AM
Burgess, in terms of the server code, yes, it's overloading. In terms of
the client, no, it's not. Unfortunately that's a limitation of WSDL.

http://www.w3.org/TR/wsdl

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


[quoted text, click to view]

Girish Bharadwaj
10/15/2003 8:34:01 AM
[quoted text, click to view]
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.


--
Girish Bharadwaj
Bubba Thomas
11/25/2003 12:44:19 PM
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 !



[quoted text, click to view]

Jim Blizzard [MSFT]
11/26/2003 4:17:13 PM
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.

[quoted text, click to view]

AddThis Social Bookmark Button