Method overloading in WCF – Part 4

Lets make two OperationContract with same name in ImyService

using System.ServiceModel;
namespace myFirstApp
{
public interface IMyService
 {
 [OperationContract]
int AddTwoNo(int intFirstNo, int intSecondNo);
 [OperationContract]
double AddTwoNo(double dbFirst, double dbSecond);
 }
}

And implement these two OperationContract in MyService.svc.cs

using System.ServiceModel;
namespace myFirstApp
{
 [ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)]
public class MyService : IMyServiceNew
 {
public int AddTwoNo(int intFirstNo, int intSecondNo)
 {
return intFirstNo + intSecondNo;
 }
public double AddTwoNo(double dbFirst, double dbSecond)
 {
return dbFirst + dbSecond;
 }
 }
}

I got an error while I am updating my service reference.
1
Error says
By default WSDL does not support method overloading, But we can achieve this by using Name property of OperationContract.

using System.ServiceModel;
namespace myFirstApp
{
 [ServiceContract(Name="IMyService")]
public interface IMyServiceNew
 {
 [OperationContract(Name="AddTwoNoInteger")]
int AddTwoNo(int intFirstNo, int intSecondNo);
 [OperationContract(Name = "AddTwoNoDouble")]
double AddTwoNo(double dbFirst, double dbSecond);
 }
}

I hope this article is helpful for you.

Thanks 🙂

Tags: ,

  • nandu

    please provide some more article of WCF