Overloading in WCF

WCF doesnot supports function Overloading . To Achive this use Name attribute in Contracts

Eg :

Class
====
public class Service : IService
{
public int GetValue(int i)
{
return i;
}

public string GetValue(string s)
{
return s;
}
}

Interface
======
[ServiceContract]
public interface IService
{

[OperationContract]
int GetValue(int i);

[OperationContract]
string GetValue(string s);
}


If you are trying to run this interface , you will get an Invalid Operation

As far as the C# is concerned, this is a completely legal piece of code. The interface contains two methods. The methods have the same name, but their signatures are unique since their parameters differ.

When WCF attempts to start the service, it essentially interrogates the metadata to generate a WSDL contract. WSDL is all about message based communication. It doesn't support object-oriented concepts such as inheritance and overloading. So, WCF basically detects there are two methods with the same name and raises an exception to indicate this isn't allowed even though the code compiled without any errors.

Here is the Complete code

Class
====
public class Service : IService
{
public int GetValue(int i)
{
return i;
}

public string GetValue(string s)
{
return s;
}
}

Interface
======
[ServiceContract]
public interface IService
{

[OperationContract(Name="GetIntValues")]
int GetValue(int i);

[OperationContract(Name = "GetStringValues")]
string GetValue(string s);
}


OperationContract(Name = "GetIntValues") will add his in the WSDL as "GetIntValues"
OperationContract(Name = "GetStringValues") wii add this method in WSDL as "GetStringValues"

For wsdl file the function name is unique & for wcf the function is Overloaded.