Overloading routines in WebServices

By Chris Gaskell

Web services are also classes just like any other .NET classes. Since a web service is a class it can utilise all the OO features like method overloading. However to use this feature with WebMethods we need toadd a little extra syntax.

Creating WebMethods
Create a WebService that has the following overloaded methods:

public string GetGreeting()
public string GetGreeting(string p_Name)
public string GetGreeting(string p_Name, string p_Message)

All these three methods return variants of a Greeting message. Now mark the methods as Web Methods, simply add the [WebMethod] attribute.

[WebMethod]
public string GetGreeting()
{
return “Hi Guest”;
}

[WebMethod]
public string GetGreeting(string p_Name)
{
return “Hi ” + p_Name + “!”;
}

[WebMethod]
public string GetGreeting(string p_Name, string p_Message)
{
return “Hi ” + p_Name + “!” + p_Message;
}

Run the WebService in the browser. That should give an error saying that the GetGreeting() methods use the same message name ‘GetGreeting’.

Adding the MessageName property.
Add the MessageName property to the WebMethod attribute as shown below:

[WebMethod]public string GetGreeting()
{
return “Hi Guest”;
}
[WebMethod (MessageName="WithOneString")]
public string GetGreeting(string p_Name)
{
return “Hi ” + p_Name + “!”;
}
[WebMethod (MessageName="WithTwoStrings")]
public string GetGreeting(string p_Name, string p_Message)
{
return “Hi ” + p_Name + “!” + p_Message;
}

Now compile the WebService and run in the browser. You can see that the first method is displayed as GetGreeting but for the second and third method the alias we set using the MessageName property is displayed.

You could of course use the alias’ as your method names – but that’d be taking the fun out of OO :-)

10 Responses to “Overloading routines in WebServices”

  1. Phil Winstanley Says:

    Woo – very clever!

    I’m off to blog about this!

  2. Plip's Weblog : Something everyone should know about web services! Says:

    [...] friend Chris posted about something that's really cool with .NET web services I never knew!http://cgaskell.wordpress.com/2007/01/19/overloading-routines-in-webservices Share this post: email it! | bookmark it! | digg it! | reddit! | kick it! | live it! Published [...]

  3. Oskar Austegard Says:

    Ok – so what’s the benefit of this? If you’re writing a dedicated webservice layer, there’s not a whole lot of point to it, and if you’re planning on using the webmethods both on the server itself and as a webservice, then you’ve just introduced a whole lot of confusion.

    Face it – there’s not much OOness about webservices, and trying to make it so is just an exercise in futility.

  4. Lee Cunningham Says:

    I think Chris’ point is to show how to overload webmethods if needed — not to suggest that we all run out there and create classes full of functions that are all named the same . You and I may come across a need to overload a function in a web service at some point in our careers, and we’ll have ol’ Chris to thank when we amaze our friends and neighbors with this little trick of his.

  5. Kevin Dente Says:

    Take care with this – at one point in the past Apache Axis didn’t correctly support overloaded methods, even if you gave them unique message names. They may have fixed that by now, but we avoid overloads to ensure maximum interoperability with different client tools.

  6. John Pearson Says:

    Nice Post.

    That was well said. Always appreciate your indepth views. Keep up the great work!

    John

  7. Chris Falter Says:

    I have tried this approach with VS 2005, and I can’t make it work. I think using MessageName to overload a WebMethod only works with VS 2003.

  8. arsenalist Says:

    I have a question. Say I want to return a complex data type using a web service. So to extend your example:

    [WebMethod]
    public Greeting GetGreeting(string p_Name)

    Instead of returning a string, we’ll return a complex object Greeting.

    If you let .NET generate the WSDL it will expose all properties of Greeting. My question is that what if I wanted to prevent one of the properties of Greeting being exposed in the WSDL. For example, if I have:

    class Greeting {
    string message;
    string from;
    string to;
    ….
    }

    How would I prevent “to” from appearing in the WSDL? Any ideas?

  9. Rakesh Says:

    Too Good and Simple way to explain webservices

  10. kishoe Says:

    very nice article. Good ,thank you

Leave a Reply