Why Method Overloading Is Not Allowed in Wcf

Why method overloading is not allowed in WCF?

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.


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

public interface MyService
int Sum(int x, int y);

int Sum(double x, double y);

Overloaded methods are not supported by WCF service?

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

UserAccount GetUser(Int32 id);

UserAccount GetUser(string username, string password);

WCF Best Practice for Overloaded methods

You can leave it like that if you like. Just use the name property of the OperationContract attribute.

interface IInterface
MyType ReadMyType(int id);
[OperationContract(Name= "Foo")]
IEnumerable<MyType> ReadMyType(String name);
[OperationContract(Name= "Bar")]
IEnumerable<MyType> ReadMyType(String name, int maxResults);

Function Overloading in WCF

WCF is designed to be a completely generic communications framework - which means that clients might not be .NET-based, and therefore might not have any understanding of method overloading.

There is functionality available for having methods with the same name, but it's a pain to get working reliably; I would strongly recommend just biting the bullet and having different method names, even though it feels 'wrong'.

How can i have two methods with same name in WCF?

    Why WCF doesnot support method overloading directly ?
  • Because WSDL doesnot support method overloading(not OOPs).
    WCF generates WSDL which specifies the location of the service and the operation or methods the service exposes.

    WCF use Document/Literal WSDL Style : Microsoft proposed this standard where the soap body element will contain the web method name.

  • By default all the WCF services conform to the document literal standard where the soap body should include the method name.

    The only way is using Name attribute. For eg,

    int Display(int a,int b)
    double Display(double a,double b)

The the compiler will generate the following, which makes sense for wsdl to locate

     [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "")]
public interface IService1
int Display(int a,int b)

double Display(double a,double b)

When using WCF, Function Overloading rule?

You could use a class as a parameter.

public class MySearchSettings

public int? ID { get; set; }
public string Name { get; set; }
public string StackOver { get; set; }


and than create a method like this:

public GetStackOverflowResponse GetStackOverflow(MySearchSettings searchSettings)
var response = new GetStackOverflowResponse();
User user = null;
if (searchSettings == null)
throw new ArgumentNullException("searchSettings");
if (searchSettings.ID.HasValue)
user = //queryByID;
else if (!String.IsNullOrEmpty(searchSettings.Name))
user = //queryByName;
else if (!String.IsNullOrEmpty(searchSettings.StackOver))
user = //queryByStackOver;
response.User = user;
catch(Exception e)
response.ErrorMessage = String.Format("{0}: {1}",
return response;

I have not included the GetStackOverflowResponse class but you get the idea of it.
One of the benefits of this is that you could easily extend the class without breaking functionality of a client when a newer version of your Service is deployed.

Method overloading in webservices

Okay for overloading:

[WebMethod(MessageName = "MaxInt", Description = "Compare two int values 
and return the max value", EnableSession = true)]
public int MaxValue(int a, int b)
return (a > b ? a : b);
[WebMethod(MessageName = "MaxFloat", Description = "Compare two float values
and return the max value", EnableSession = true)]
public float MaxValue(float a, float b)
return (a > b ? a : b);

What do you mean precisely by authentication? You can obviously use a validation key to access webservice. The question is confusing. Elaborate please.

Related Topics

Leave a reply