WCF Overloading - OperationContract(Name=DifferentMethodNames)


 
Today I will tell you how to obtain method overloading in WCF.

Just to give a little background, we cannot implement method overloading in WCF.
This is because WSDL(Web Service Definition Language) does not support features of OOPs.

If you don’t agree with me, try creating method overloading in WCF and run the same.
Without fail it will give InvalidOperationException. J

Now here we go on to implement the Method overloading WCF.

First created your overload as you do in normal C# application in Service file.

Here is the sample I have used

public class Service1 : IService1
    {
        public string Add(int v1, int v2)
        {
            return (v1 + v2).ToString();
        }

        public string Add(string v1, string v2)
        {
            return (v1 + v2);
        }
    }

Go into the Iservice1.cs

And declare the method as usual.
Just make sure to provide different Value to Name attribute to OperationContract
See the example below

        [OperationContract(Name = "AddInt")]
        string Add(int v1, int v2);

        [OperationContract(Name = "AddString")]
        string Add(string v1, string v2);

Now create a simple Console application and add the service just created.

Create an instance of the service.
Refer to image below.













We can clearly see the two method just created.

One thing to catch here.

The method name exposed on the client side is not the one created on server side but the Value
we assign to the Name attribute to OperationContract

Now you will say where is Method overloading. Very true, as of now we have just be able to perform method overloading on service side.
To reflect the same on client side we need to do another little trick.
Here we go.

Go to Service Reference.cs file 

Navigate to  public partial class Service1Client :System.ServiceModel.ClientBaseIService1>, WCFSampleTest.ServiceReference1.IService1

There you will find the two method that we have create in service.

  public string AddInt(int v1, int v2) {
            return base.Channel.AddInt(v1, v2);
        }
       
  public string AddString(string v1, string v2) {
            return base.Channel.AddString(v1, v2);
        }

Just go and change the method name from to
As displayed below.

  public string Add(int v1, int v2) {
            return base.Channel.Add(v1, v2);
        }
       
  public string Add(string v1, string v2) {
            return base.Channel.Add(v1, v2);
        }

Now as this Class ServiceClient inherits the IService interface
We need to go and change the Interface acoordingly.


Here we go


[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService1/AddInt", ReplyAction="http://tempuri.org/IService1/AddIntResponse",Name="AddInt")]
        string Add(int v1, int v2);       
        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService1/AddString", ReplyAction="http://tempuri.org/IService1/AddStringResponse",Name="AddString")]

Sometimes the 3rd Named parameter Name does not appear, if that happens go ahead and add manually.
And now your job is done.

You will now be able to see the the acutual methodOverload on the client side as well

Refer to below image









Happy Coding J
 

Comments

Popular posts from this blog

ASP.NET Compillation Error BC31007 - Solution

The Difference between GET and POST

Test & Debug WCF service using WCFTestClient.exe