Method Overloading in WCF: Part 4

Before reading this article, I highly recommend reading the previous parts:
Let's make two OperationContract with same name in ImyService. 
  1. using System.ServiceModel;  
  2.   
  3. namespace myFirstApp  
  4. {  
  5.     public interface IMyService  
  6.     {  
  7.      [OperationContract]  
  8.         int AddTwoNo(int intFirstNo, int intSecondNo);  
  9.        
  10.      [OperationContract]  
  11.         double AddTwoNo(double dbFirst, double dbSecond);  
  12.     }  
  13. }  
And implement these two OperationContracts in MyService.svc.cs:
  1. using System.ServiceModel;  
  2.   
  3. namespace myFirstApp  
  4. {  
  5.     [ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)]  
  6.     public class MyService : IMyServiceNew  
  7.     {  
  8.         public int AddTwoNo(int intFirstNo, int intSecondNo)  
  9.         {  
  10.             return intFirstNo + intSecondNo;  
  11.         }  
  12.   
  13.         public double AddTwoNo(double dbFirst, double dbSecond)  
  14.         {  
  15.             return dbFirst + dbSecond;  
  16.         }  
  17.     }  
  18. }  
I got an error while updating my service reference.
 
 
 
Error says: cannot have two operations in the same contract with the same name. You can change the name of an operation by changing the method name or using the name property of OperationContractAttribute.
 
By default WSDL does not support operational overloading, but we can do this using the Name property of OperationContract.
  1. using System.ServiceModel;  
  2.   
  3. namespace myFirstApp  
  4. {  
  5.     [ServiceContract(Name="IMyService")]  
  6.     public interface IMyServiceNew  
  7.     {  
  8.         [OperationContract(Name="AddTwoNoInteger")]  
  9.         int AddTwoNo(int intFirstNo, int intSecondNo);  
  10.   
  11.         [OperationContract(Name = "AddTwoNoDouble")]  
  12.         double AddTwoNo(double dbFirst, double dbSecond);  
  13.     }  
  14. }  
Now update your service reference and run the application. It will work fine.
 
I hope this article is helpful to you.
 
Thanks :)
 


Similar Articles