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.
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.
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.
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);
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);
Try this:
- [OperationContract(Name= "GetUserWithID")]
UserAccount GetUser(Int32 id);
- [OperationContract(Name= "GetUserWithUserName")]
UserAccount GetUser(string username, string password);
More Info
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With