Can web methods be overloaded ?

Discussion in 'ASP .Net Web Services' started by john bailo, Oct 15, 2003.

  1. john bailo

    john bailo Guest

    Can a web method be overloaded?

    To support varying numbers of input parameters?
     
    john bailo, Oct 15, 2003
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    >
    > Can a web method be overloaded?
    >
    > To support varying numbers of input parameters?
    >
    >
    >
     
    Greg Ewing [MVP], Oct 15, 2003
    #2
    1. Advertising

  3. john bailo

    john bailo Guest

    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 [MVP]" <gewing@_NO_SPAM_gewing.com> wrote in message
    news:%...
    > 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" <> wrote in message
    > news:...
    > >
    > > Can a web method be overloaded?
    > >
    > > To support varying numbers of input parameters?
    > >
    > >
    > >

    >
    >
     
    john bailo, Oct 15, 2003
    #3
  4. john, the client will have to use Add2.

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

    "john bailo" <> wrote in message
    news:...
    > 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 [MVP]" <gewing@_NO_SPAM_gewing.com> wrote in message
    > news:%...
    > > 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" <> wrote in message
    > > news:...
    > > >
    > > > Can a web method be overloaded?
    > > >
    > > > To support varying numbers of input parameters?
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Greg Ewing [MVP], Oct 15, 2003
    #4
  5. john bailo

    Chris Taylor Guest

    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" <> wrote in message
    news:...
    >
    > Can a web method be overloaded?
    >
    > To support varying numbers of input parameters?
    >
    >
    >
     
    Chris Taylor, Oct 15, 2003
    #5
  6. Greg Ewing [MVP] dribbled:

    > john, the client will have to use Add2.


    Is that 'overloaded' then?

    Isn't that just the same as two methods?
     
    Burgess Meredith, Oct 15, 2003
    #6
  7. 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 Meredith, Oct 15, 2003
    #7
  8. 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


    "Burgess Meredith" <> wrote in message
    news:...
    > Greg Ewing [MVP] dribbled:
    >
    > > john, the client will have to use Add2.

    >
    > Is that 'overloaded' then?
    >
    > Isn't that just the same as two methods?
    >
    >
    >
     
    Greg Ewing [MVP], Oct 15, 2003
    #8
  9. Burgess Meredith wrote:

    > 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.
    >
    >

    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
     
    Girish Bharadwaj, Oct 15, 2003
    #9
  10. john bailo

    Bubba Thomas Guest

    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" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:...
    > >
    > > Can a web method be overloaded?
    > >
    > > To support varying numbers of input parameters?
    > >
    > >
    > >

    >
    >
     
    Bubba Thomas, Nov 25, 2003
    #10
  11. 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" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:...
    > > 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" <> wrote in message
    > > news:...
    > > >
    > > > Can a web method be overloaded?
    > > >
    > > > To support varying numbers of input parameters?
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Jim Blizzard [MSFT], Nov 27, 2003
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    17
    Views:
    1,736
    Chris Riesbeck
    Nov 3, 2003
  2. Michael Borgwardt

    Generics: how to avoid overloaded methods?

    Michael Borgwardt, Dec 2, 2004, in forum: Java
    Replies:
    5
    Views:
    369
    Michael Borgwardt
    Jan 5, 2005
  3. Oliver Brausch

    Overloaded Methods called with null

    Oliver Brausch, Dec 27, 2005, in forum: Java
    Replies:
    20
    Views:
    816
    John C. Bollinger
    Dec 30, 2005
  4. Replies:
    2
    Views:
    109
  5. Kenneth McDonald
    Replies:
    5
    Views:
    375
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page