Click here to Skip to main content
15,881,380 members
Articles / Programming Languages / C#
Tip/Trick

Method overloading in WCF

Rate me:
Please Sign up or sign in to vote.
3.25/5 (8 votes)
19 Dec 2010CPOL 44.2K   4   4
Method overloading in WCF
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.

License

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


Written By
Software Developer (Senior)
India India

Microsoft C# MVP (12-13)



Hey, I am Pranay Rana, working as a Team Leadin MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.

For me def. of programming is : Programming is something that you do once and that get used by multiple for many years

You can visit my blog


StackOverFlow - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr

Awards:



Comments and Discussions

 
GeneralMy vote of 5 Pin
parminder sidhu30-Jan-14 1:06
parminder sidhu30-Jan-14 1:06 
GeneralMy vote of 1 Pin
Marcelo Lujan [El Bebe.Net ]2-May-12 13:43
Marcelo Lujan [El Bebe.Net ]2-May-12 13:43 
GeneralReason for my vote of 2 If all calls to the method have to u... Pin
Ryan Gamal16-Jan-11 23:36
Ryan Gamal16-Jan-11 23:36 
GeneralReason for my vote of 5 Good One! Pin
Anupama Roy11-Jan-11 18:22
Anupama Roy11-Jan-11 18:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.