Why method overloading is not allowed in WCF?

Sleiman Jneidi picture Sleiman Jneidi · Apr 23, 2012 · Viewed 27.8k times · Source

Assume that this is a ServiceContract

[ServiceContract]
public interface MyService
{
    [OperationContract]
    int Sum(int x, int y);

    [OperationContract]
    int Sum(double x, double y);

}

Method overloading is allowed in C#, but WCF does not allow you to overload operation contracts The hosting program will throw an InvalidOperationException while hosting

Answer

David Z. picture David Z. · Apr 23, 2012

In a nutshell, the reason you cannot overload methods has to do with the fact that WSDL does not support the same overloading concepts present inside of C#. The following post provides details on why this is not possible.

http://jeffbarnes.net/blog/post/2006/09/21/Overloading-Methods-in-WCF.aspx

To work around the issue, you can explicitly specify the Name property of the OperationContract.

[ServiceContract]
public interface MyService
{
    [OperationContract(Name="SumUsingInt")]
    int Sum(int x, int y);

    [OperationContract(Name="SumUsingDouble")]
    int Sum(double x, double y);
}