c# - 我怎样才能在 WCF 中有两个同名的方法?

标签 c# .net wcf

<分区>

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

我正在从事一个使用 WCF 服务的项目。我的问题是,在 WCF 服务中,我有一个名为 Display() 的方法,它由我的 client1 使用。

现在我想添加另一个同名但有一个参数的方法,即。 Display(string name),让新clinet2使用新方法,旧client1使用旧方法。我怎样才能做到这一点?这是我编写的代码。

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;
      }
   }
}

最佳答案

    Why WCF doesnot support method overloading directly ?
  • 因为 WSDL 不支持方法重载(不是 OOP)。 WCF 生成 WSDL,它指定服务的位置以及服务公开的操作或方法。

    WCF 使用 Document/Literal WSDL Style:Microsoft 提出了此标准,其中 soap 主体元素将包含 Web 方法名称。

  • 默认情况下,所有 WCF 服务都符合文档文字标准,其中 soap 主体应包含方法名称。

    唯一的方法是使用 Name 属性。例如,

        [OperationContract(Name="Integers")]
        int Display(int a,int b)
        [OperationContract(Name="Doubles")]
        double Display(double a,double b)
    

编译器会生成如下内容,方便wsdl定位

     [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)
  }

关于c# - 我怎样才能在 WCF 中有两个同名的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14472918/

相关文章:

asp.net - ADFS 2.0 身份验证的 Web 服务调用

c# - IIS 托管 WCF 服务和使用 Windows 身份验证的 SQL 查询

c# - 套接字发送null的长字符串

c# - 如何使用 Dapper 检查空值

.net - 抓取标签之间的所有 html

c# - WebServices 失败,因为 (400) Bad Request because of special character

c# - WCF 性能、延迟和可伸缩性

c# - Rhino 模拟 AAA 快速入门?

c# - 在 C# 中解析自定义文件

c# - ASP.net MVC 4 单元测试 Viewbag/ViewModel 测试