This snippet will show you how to add overloaded functions to your WCF service contracts.
In our introductory WCF tutorial, I created a basic service contract that accepted a string and returned a reversed version of it. For this tutorial, I want to overload this function to also accept a character array. If I were to attempt to just stick in my overload like this:
public interface IStringReverser
{
[OperationContract]
string ReverseString(string value);
[OperationContract]
string ReverseString(char[] value);
}
It would result in this exception being thrown at runtime:
By default, every operation contract gets a name that is set to the name of method. In WCF, you cannot have two operation contracts with the same name. Fortunately, this is very easy to fix by simply setting the name property of the operation contract attribute.
public interface IStringReverser
{
[OperationContract(Name = "ReverseStringFromString")]
string ReverseString(string value);
[OperationContract(Name = "ReverseStringFromCharArray")]
string ReverseString(char[] value);
}
And there you have it. We now have a WCF service contract with overloaded methods.
Add Comment
[language] [/language]
Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]
See here for supported languages.
Javascript must be enabled to submit anonymous comments - or you can login.