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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • QA teams don't have time to test everything yet they can't afford to ship buggy code. Learn how Coverity can help organizations shrink their testing cycles and reduce regression risk by focusing their manual and automated testing based on the impact of change.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds