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,

        [OperationContract(Name="Integers")]
        int Display(int a,int b)
        [OperationContract(Name="Doubles")]
        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", "3.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(ConfigurationName=
    "ServiceRef.IService1")]
  public interface IService1
   {
       [System.ServiceModel.OperationContractAttribute(
       Action="http://tempuri.org/Service1/AddNumber",
       ReplyAction="http://tempuri.org/IHelloWorld/IntegersResponse")]                   
       int Display(int a,int b)

       [System.ServiceModel.OperationContractAttribute(
       Action="http://tempuri.org/IHelloWorld/ConcatenateStrings",
       ReplyAction="http://tempuri.org/Service1/DoublesResponse")]
       double Display(double a,double b)
  }

OK, I'm going to make this an answer, since the comments get overly excessive by now.

You basically have two options:

  • Use a single interface (note that interface inheritance, like you suggest in your question, technically counts as one interface here), but then you have to give each service operation a distinct name. You can either do that by naming the C# methods distinct, or by applying the [OperationContract(Name = "distinctname")] attribute.

  • Use two separate interfaces, without any inheritance relationship in between them, publishing each on a different endpoint. You can have then have a service operation in each, having the same name, but with different parameters. You can still implement both interfaces with one implementation class, if you like/need to, of course.

Tags:

C#

.Net

Wcf