Tuesday, May 19, 2015

Method Overloading in WCF: Part 5


Please read my previous article :


  WCF Service-Part 1

 WCF Features-Part 2

 ABC of WCF-Part 3
 wcf demo appication-part 4

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.

If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website