Problem
I found a scenario where I have to implement two methods with the same name, i.e Function overloading. But when I implement that, I got an error "
System.InvalidOperationException"
System.InvalidOperationException: Cannot have two operations in the same
contractwith the same name, methods GetData and GetData in type
WcfServiceLibrary.IService1 violate this rule.
Solution
To resolve this issue, you just need to use a meaningful name for the method by using
NAME field available in
OperationContract attribute. So you need to give a name to both or one of the methods to avoid this problem. As you can see in the below code that I did in control
IService.cs interface.
[OperationContract(Name = "GetIntegerData")]
string GetData(int value);
[OperationContract(Name = "GetStringData")]
string GetData(string value);
So after doing the change, I get live service but the name of the method gets change by the name specified in the
Name attribute which you can see in the below image:
So to consume service in my client application, you need to use the method name you specified in the
Name attribute. The code in my client application is:
ServiceReference1.Service1Client client = new Client.ServiceReference1.Service1Client();
Console.WriteLine(client.GetIntegerData(5));
Console.WriteLine(client.GetStringData("pranay"));
Summary
You can easily resolve the issue of method overloading in WCF service with the help of
Name field in
OperationContract attribute.