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

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

  • Email is the most common communication vehicle used by organizations of all shapes and sizes. Among the billions of email messages sent every day are sensitive information, critical requests, and other essential business data. IT staff bear the burden of ensuring the confidentiality, integrity, and availability of the information contained within the communication. This white paper explores the email security landscape, an assessment of the threats organizations face,  and the building blocks of an effective …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds