Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

wcf

Possible Duplicate:
why you cant overload a method in WCF?

I am working on one project in which I use the WCF services. My problem is that in WCF service I am having one method named Display() which is used by me client1.

Now I want to add another method with the same name but with one parameter, ie. Display(string name), so that new clinet2 can use the new method and old client1 can use the old method. how can I achieve this? Here is the code which I have written.

namespace ContractVersioningService
{
  [ServiceContract]
  public interface IService1 
  {
    [OperationContract]
    string Display();

    [OperationContract]
    string GoodNight();
  }     
}

namespace ContractVersioningService
{
   public class Service1 : IService1
   {
     public string Display()
     {
        return "Good Morning";          
     }

     public string GoodNight()
     {
        return "Good Night";
     }
   }
}    

namespace ContractVersioningService
{
  [ServiceContract(Namespace = "ContractVersioningService/01", Name =      "ServiceVersioning")]
  public interface IService2 : IService1
  {
     [OperationContract]
     string Disp(string greet);
  }
}

namespace ContractVersioningService
{
 
   public class Service2 : Service1, IService2
   {
      public string Display(string name)
      {
         return name;
      }

      public string Disp(string s)
      {
         return s;
      }
   }
}
like image 263
Sagar Avatar asked Jan 23 '13 05:01

Sagar


2 Answers

    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)
  }
like image 187
C-va Avatar answered Sep 28 '22 05:09

C-va


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.

like image 36
Christian.K Avatar answered Sep 28 '22 03:09

Christian.K