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: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds