WCF: Operation Overloading inside Service Contracts using C#


In this post, I will explain a simple way to do operation overloading for Service Contracts in a WCF Service.

If you create WCF Services, you might already know about ServiceContract attribute. It just describes the operations that a client can perform over a service and if you are from Object oriented programming background, you know that programming languages support Method Overloading. It is as simple as having two or more methods with the same name but with different parameters in same scope. The advantage is that we can perform same operations on different datatypes without having the need to use separate names for each version.

public Interface ICalc
    {
      Int Subtract (int a, int b);
      double Subtract (double a, double b);
    }

Well the above code is fine with C++ for overloading. if you try to do the same thing with WCF like below, 

[ServiceContract]
    public interface ICalc
    {
        [OperationContract]
        int Subtract(int x, int y);

        [OperationContract]
        double Subtract(double x, double y);
      
    }
public class Service1 : ICalc
    {
        public int Subtract(int x, int y)
        {
            return x - y;
        }

        public double Subtract(double x, double y)
        {
            return x - y;
        }
    }
it compiles but you would end up seeing a InvalidOperationException at the service host load time!


“Cannot have two operations in the same contract with the same name, methods Subtract and Subtract in type xyz violate this rule. You can change the name of one of the operations by changing the method name or by using the Name property of OperationContractAttribute.”

The reason is simple; Operations are only identified by names and messages in this particular world.

Now, to get rid of this problem, just manually enable the operation overloading. Simple. Use the Name property of the OperationContract Attribute to alias the operation.

[AttributeUsage(AttributeTargets.Method)]
    Public sealed class OperationContractAttribute : Attribute
    {
      Public string Name
        {get;set;}
    }

However, you need to alias both at Server and the Client Side. Here is how you give a unique name at server side

[ServiceContract]
    public interface ICalc
    {
        [OperationContract(Name = "SubInt")]
        int Subtract(int x, int y);

        [OperationContract(Name="SubDouble")]
        double Subtract(double x, double y);
    }

Now, When the client generates the proxy the operations will have the alias names

[ServiceContract]
    public interface ICalc
    {
        [OperationContract]
        int SubInt(int x, int y);

        [OperationContract]
        double SubDouble(double x, double y);
       
    }
Now, the client can use the generated proxy as above. But this is not so cool! The overloading has not been done to use with two different name by the client.

To make it even better, rename the methods on the imported contract and the proxy to overloaded names. The proxy class should call on the internal proxy using overloaded methods and use the Name property and imported contract on client side to complete it. Now we have overloading at client side.

[ServiceContract]
     public Interface ICalc
     {
        [OperationContract(Name = "SubInt")]
        int Subtract(int x, int y);

        [OperationContract(Name="SubDouble")]
        double Subtract(double x, double y);
     }

     class CalcClient: Servicereferce1.CalcClient
     {
        public int Subtract (int a, int b)
        {
          return Channel.SubInt(a,b);
        }

        public double Subtract (double a, double b)
        {
          return Channel.SubDouble(a,b);
        }
     }

Now it is easy to use the overloaded operations without any confusions!

CalcClient pxy = new CalcClient();
    int output1=pxy.Subtract(3,2);
    double output2= pxy.Subtract(3.0, 2.0);
    pxy.Close(); 

Thats it.Very simple and straight. Happy coding :)

Lets do programming together. You can follow me on twitter @MSGuyTweets or find me on Facebook at Facebook.com/MysoreGuy

Popular posts from this blog

Facebook Javascript API : Feed and Share Dialog for Beginners

Real time Push Notifications with SignalR & PNotify (Pines Notify)

What's new and expected in .NET Framework 4.5