Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know WCF does not support method overloading and to achieve it we need to decorate the operationcontract with name attribute i.e.

C#
[ServiceContract ]
public interface ITest
{
    [OperationContract(Name="Method1")]
    string TestMethod(int para1,int para2);
    //Initail method
    [OperationContract(Name = "Method2")]
    string TestMethod(string para1, string para2);
    //Overloading on the basis of type of parameters.
    [OperationContract(Name = "Method3")]
    string TestMethod(int para1, string para2);
    //Overloading on the basis of  an order of parameters.
    [OperationContract(Name = "Method4")]
    string TestMethod(int para1, string para2,double para3);
    //Overloading on the basis of the numbers of parameters
}

my question is how it is method oveloding as we need to call second TestMethod with Method2 whenever we want to call that method so why decorate with attribute and why not simply write like Method1,Method2 etc.
what is the benefit as a developer we are getting to use the Name attribute ??
Posted
Updated 28-Mar-14 3:01am
v2

1 solution

You could, as you say name the methods "Method1", "Method2" instead of decorating as per your example code. From the client side it appears no different.
The benefit is really on the client side, the overridden methods have a semantic meaning - these methods do the same allow for the different input parameters. Code using ITest directly (not via WCF) calls via the actual method names. With WCF you always have to be aware that the service is written in OO-based code, but the service calls itself isn't OO and there is a mismatch here.

I haven't used WCF in anger in a while - but I did used to try as far as possible to keep the contract name (the one that is passded up and down the wire) and the actual method name in the service code the same. (Same for data contracts) otherwise I found it harder to trace problems through etc.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900