<a href="http://www.micropoll.com/akira/mpview/585320-168921">Click Here for Poll</a><a href="http://www.questionpro.com" title="online surveys">Online Survey</a><BR> | <a href="http://www.micropoll.com" title="Website Polls">Website Polls</a><BR> | <BR><a href="http://www.micropoll.com/akira/MicroPoll?mode=html&id=168921">View MicroPoll</A></div>

Visual Studio 2010!

Read now >

Windows Mobile Development Thoughts

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.
ADVERTISEMENT
ADVERTISEMENT

 

DevSource.com: Your Source for Visual Studio on Facebook
ADVERTISEMENT
Web Methods Overloading in .NET
By Max Loukianov

Rate This Article: Add This Article To:

Web Methods Overloading in .NET - ' Doing It In '
( 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.



 
 
>>> More Using Microsoft Visual Studio Articles          >>> More By Max Loukianov