Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloaded methods are not supported by WCF service?

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.

like image 814
A.T. Avatar asked Mar 07 '13 10:03

A.T.


People also ask

Can we overload methods in WCF?

So clearly by default WCF does not allow you to overload methods in service. However there is one attribute in OperationContract which can allow you to overload method at service end. You need to set Name parameter of OperationContract to manually enable overloading at service side.

Is overloading possible in Web services?

This is a very common interview question as well: Is it possible to overload a web method in a web service? The answer is yes, you need to use MessageName property for this.


3 Answers

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);
like image 148
Jehof Avatar answered Oct 29 '22 00:10

Jehof


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);
like image 31
Alex Avatar answered Oct 29 '22 00:10

Alex


Try this:

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

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

More Info

like image 21
Flowerking Avatar answered Oct 29 '22 01:10

Flowerking