How to Implement Method overloading in WCF

No.of Views2780
Bookmarked0 times
Downloads 
Votes0
By  pranay rana   On  01 Mar 2011 09:03:17
Tag : WCF , General
I found scenario where I have to implement two method with the same name i.e Function overloading. But when I implement that I got error "System.InvalidOperationException"
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Problem

I found scenario where I have to implement two method with the same name i.e Function overloading. But when I implement that I got 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.

 

Image Loading

Solution

To resolve this issue you just need to use meaning full name for the method by using NAME field available in OperationContract attribute. So you need to give name to both or one of the method to avoid this problem. As you can see in 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 get change by the name specified in the  Name attribute which you can see in below image

 

Image Loading

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 issue of method over loading in WCF service with the help of Name field in OperationContract attribute.

 
Sign Up to vote for this article
 
About Author
 
pranay rana
Occupation-CEO
Company-GMind Solusion
Member Type-Expert
Location-India
Joined date-08 Jan 2011
Home Page-http://pranayamr.blogspot.com
Blog Page-http://pranayamr.blogspot.com
Hey, I am Pranay Rana, working as a Senior Software engineer in mid-size company located in ahmedabad. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 4.3 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 me on my blog - http://pranayamr.blogspot.com/ StackOverFlow - http://stackoverflow.com/users/314488/pranay My CV :- http://careers.stackoverflow.com/pranayamr
 
 
Other popularSectionarticles
    Comments
    There is no comments for this articles.
    Leave a Reply
    Title:
    Display Name:
    Email:
    (not display in page for the security purphase)
    Website:
    Message:
    Please refresh your screen using Ctrl+F5
    If you can't read this number refresh your screen
    Please input the anti-spam code that you can read in the image.
    ^ Scroll to Top