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

  • As mobile devices have pushed their way into the enterprise, they have brought cloud apps along with them. This app explosion means account passwords are multiplying, which exposes corporate data and leads to help desk calls from frustrated users. This paper will discover how IT can improve user productivity, gain visibility and control over SaaS and mobile apps, and stop password sprawl. Download this white paper to learn: How you can leverage your existing AD to manage app access. Key capabilities to …

  • For the third consecutive year, Neustar surveyed hundreds of companies on distributed denial of service (DDoS) attacks. Neustar's survey reveals evidence that the DDoS attack landscape is changing. The number of companies attacked is up, but attack duration is down. Larger attacks are more common, but most attacks are still less than 1 Gbps. More than half of attacked companies reported theft of funds, data or intellectual property. Read the full report to learn how DDoS attacks have evolved in both strategy …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds