Click here to Skip to main content
Click here to Skip to main content

Tagged as

Method overloading in WCF

, 19 Dec 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Pranay Rana
Software Developer (Senior) GMind Solusion
India India

Microsoft C# MVP (12-13)

 
Hey, I am Pranay Rana, working as a ITA in 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:


Follow on   Twitter   LinkedIn

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberparminder sidhu30-Jan-14 2:06 
GeneralMy vote of 1 [modified] PinmemberMarcelo Lujan [El Bebe.Net ]2-May-12 14:43 
GeneralReason for my vote of 2 If all calls to the method have to u... PinmemberRyan Gamal17-Jan-11 0:36 
GeneralReason for my vote of 5 Good One! PinmemberAnupama Roy11-Jan-11 19:22 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 19 Dec 2010
Article Copyright 2010 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid