10

i have two methods named as

[OperationContract]
UserAccount GetUser(Int32 id);

[OperationContract]
UserAccount GetUser(string username, string password);

when i try to build them, they said you can not have same name methods in service ? Is it.

A.T.
  • 24,694
  • 8
  • 47
  • 65
  • 1
    Note: [Polymorphism](http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming) is different than overloading. Also, please include the *exact* error message in questions as it makes them a bit more searchable (if someone searches for said error message). –  Mar 07 '13 at 10:14
  • Overloading is an implementation of polymorphism. Polymorphism is an abstract concept. – A.T. Mar 07 '13 at 10:17
  • Overloading is orthogonal to polymorphism and exists in some languages which do not support [subtype] polymorphism. Overloading is also absent in some language that do support [subtype] polymorphism. Overriding is generally associated with [subtype] polymorphism, but I digress .. –  Mar 07 '13 at 10:18
  • Overloading is practical approach to achieve Polymorphism what i believe....anyway its a two ways to one destination :p – A.T. Mar 07 '13 at 10:24
  • No. [Overloading](http://en.wikipedia.org/wiki/Function_overloading) is *not* related to polymorphism: "*Method overloading should not be confused with [..] polymorphism* where the correct method is chosen at runtime, e.g. through virtual functions, instead of statically." –  Mar 07 '13 at 10:25
  • (Actually, I can't find a "non-OO" language with function overloading but, as [seen in this C++ example](http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr312.htm), no polymorphism is required to use function overloading in a language that supports it.) –  Mar 07 '13 at 10:27
  • Is this still not working even if the return types are different? for example, what if my second function returns string instead of UserAccount Object? – Emil Nov 11 '13 at 14:26
  • possible duplicate of [Why method overloading is not allowed in WCF?](http://stackoverflow.com/questions/10276124/why-method-overloading-is-not-allowed-in-wcf) – Michael Freidgeim Feb 02 '15 at 00:50

4 Answers4

14

This is a limitation of WSDL. It does not support the same overloading concepts as C#/.NET, so that method names on services have to be unique. You have two option to resolve your problem.

First one is to use diffrent names for your methods. The other one is to set the Name property on one of your OperationContracts like so

[OperationContract(Name="GetUserById")]
UserAccount GetUser(Int32 id);

[OperationContract]
UserAccount GetUser(string username, string password);
Jehof
  • 34,674
  • 10
  • 123
  • 155
  • gr8 thanks...so now i will recognize it as GetUserById or GetUser, am i ryt – A.T. Mar 07 '13 at 10:13
  • 3
    @Arun The name is used for WSDL description of your WCF Service. If you use a normal WCF-Client it should provide the overloaded methods as you defined them on your interface – Jehof Mar 07 '13 at 10:15
4

WSDL does not support the same overloading concepts of c#. You can use Name in your OperationContract to specify your methods

 [OperationContract(Name="GetUserInt")]
 UserAccount GetUser(Int32 id);

 [OperationContract(Name="GetUserString")]
 UserAccount GetUser(string username, string password);
Alex
  • 8,827
  • 3
  • 42
  • 58
2

Try this:

- [OperationContract(Name= "GetUserWithID")]
   UserAccount GetUser(Int32 id);

- [OperationContract(Name= "GetUserWithUserName")]
  UserAccount GetUser(string username, string password);

More Info

Flowerking
  • 2,551
  • 1
  • 20
  • 30
0

This is the drawback of WCF. Reason behind this is that when we relies the service for client then there should be no duplication, otherwise client will confuse which method is doing what? So here are the options to resolve the problem. By using the different name attribute of Operation Contract.

[OperationContract(Name="GetUserByID")]

UserAccount GetUser(Int32 id);

[OperationContract(Name="GetUserByUName_Password")]

UserAccount GetUser(string username, string password);