2005-06-15
| Table of Contents: |
| Rate This Article: | Add This Article To: |
( Page 4 of 4 )
.NET">
Overloading Web Methods in .NET
All those WSDL we just saw changes are nice, but the beauty of .NET is that we can expose a method as a Web Method by adding a WebMethod attribute to the method declaration. Nowadays, developers rarely write WSDL by hand, so let's have a look and find out if method overloading is possible for Web Methods.
Overloading is an OO concept. Web Services is a message-based architecture and does not implement all the facets of object-oriented world by default. As we just saw, default WSDL required substantial modifications to support overloaded method calls from the client.
Luckily, there is the MessageName property of a WebMethod attribute! The MessageName property enables the XML Web Service to uniquely identify overloaded methods using an alias. The default value of this property is the method name, but you can change it to distinguish this overloaded method impkementation from others. When specifying the method name, the resulting SOAP messages will reflect this name instead of an actual method name. Here is an example of using this property in our PrettyPrinter class:
using System;
class PrettyPrinter
{
[System.Web.Services.WebMethod(MessageName="PrintBankCard")]
public string print(BankCard card)
{
return card.owner + "\'s " + card.brand
+ " Num " + card.number + "; exp " + card.expDate;
}
[System.Web.Services.WebMethod(MessageName="PrintCheckingAccount")]
public string print(CheckingAccount account)
{
return account.owner + "\'s checking account No " + account.number;
}
}
The resulting SOAP message and response will use PrintBankCard and PrintCheckingAccount accordingly. Again, the attributes in .NET prove to be extremely useful! It is also worth mentioning that we actually specified distinct names for methods, via the MessageName property; during refactoring of this code, it would be worth considering changing the names so that they reflect the message names. After refactoring, our PrettyPrinter class would look like the following:
using System;
class PrettyPrinter
{
[System.Web.Services.WebMethod]
public string printBankCard(BankCard card)
{
return card.owner + "\'s " + card.brand +
" Num " + card.number + "; exp " + card.expDate;
}
[System.Web.Services.WebMethod]
public string printCheckingAccount(CheckingAccount account)
{
return account.owner + "\'s checking account No " + account.number;
}
}
Now everything is in line and looks great!
Latest Developments
Method overloading has been removed from the WSDL 1.2 specification draft. In addition WS-I Basic Profile 1.0 has no method overloading. According to the WS-I Basic Profile, there can be only one bound port and operation corresponding to a name. Differing these operations by method parts is not allowed; the names must be distinct. By refactoring our PrettyPrinter class, we did just that.
![]() |
|


