How to Achieve Operational Overloading in WCF

Service-Side Operational Overloading

In WCF, operational overloading is not acceptable. It has a different way of implementing operational overloading. Consider the following piece of code.

[ServiceContract]
interface InterfaceName
{
   [OperationContract]
   int MethodName (int paramName1, int ParamName2);

   [OperationContract]
   double MethodName (double paramName1,double ParamName2);
}

This code will throw InvaildOperationException.

You can use operational overloading with the help of the Name property of the OperationContract attribute to alias the operation. On the server and client sides, use the same name for the overloaded operations. Consider the following example:

[ServiceContract]
interface IInterfaceName
{
   [OperationContract (Name = "aliasName1")]
   int MethodName (int param1, int param2);

   [OperationContract (Name = "aliasName2")]
   double MethodName (double param1, double param1);
}

This code is used for service-side operation overloading. When you generate the proxy, the operation will have the same name that you used as the alias. You can find these methods with Names such as "aliasName1" or "aliasName2" on the proxy. Consider the following example.

// for MethodName which returns the integer value.
proxyobject.aliasName1 (1, 1)
// for MethodName which returns the double value.
proxyobject. aliasName2 (1.2, 2.1)

It seems the problem arises because a developer may get confused about these function names. That's why you need to perform the same thing on the client side.

If you need to overcome the problem, you need to use the same name property on the client side also, with the same imported operation names.

[ServiceContract]
interface IInterfaceName
{
   [OperationContract (Name = "aliasName1")]
   int MethodName (int param1, int param2);

   [OperationContract (Name = "aliasName2")]
   double MethodName (double param1, double param1);
}

public partial class Client : ClientBase< IInterfaceName >,
   IInterfaceName
{
   public int MethodName (int arg1, int arg2)
   {
      return Channel. MethodName (arg1, arg2);
   }
   public double MethodName (double arg, double arg2)
   {
      return Channel. MethodName (arg1, arg2);
   }
}

Now, on the client side you can create an object and use the benefits of operational overloading.

Client proxy = new Client ( );

int result1    = proxy. MethodName (1, 2);
double result2 = proxy. MethodName (1.0, 2.0);

Reference

Programming WCF Services by Juval Lowy. Published by O'Reilly. ISBN: 0596526997



About the Author

Dinesh Tawar

Dinesh is an independent author, and a Software Developer. He has 4.5 years of IT experience implementing Client/Server, Database, and Web based systems. He has developed systems for the ERP.Since 2002, he has been working with C#, Visual Basic .NET, Web Services, Windows Forms, GDI+, SQL Server 2005 and Visual Studio 2005. Currently he is working with Windows Vista programming using .NET 3.0, XAML, WPF, WCF. He has authored other .Net web site. CONTACT EMAIL: tawar_dinesh@yahoo.com

Comments

  • WCF

    Posted by Varsha on 06/16/2012 11:37am

    Thank You...

    Reply
  • WCF Service Method Overloading

    Posted by sandeep.cs3 on 07/09/2010 06:22pm

    Hello All, Below is the link that provide good explanation of possibility of Method overloading and trick to achieve Method overloading in SVC http://aspdotnethacker.blogspot.com/2010/07/wcf-service-method-overloading.html

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 17, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this upcoming eSeminar and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the …

  • This ESG study by Mark Peters evaluated a common industry-standard disk VTl deduplication system (with 15:1 reduction ratio) versus a tape library with LTO-5, drives with full nightly backups, over a five-year period.  The scenarios included replicated systems and offsite tape vaults.  In all circumstances, the TCO for VTL with deduplication ranged from about 2 to 4 times more expensive than the LTO-5 tape library TCO. The paper shares recent ESG research and lots more. 

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds