怎样才可以有两种方法具有相同名称的WCF?才可以、有两种、名称、方法

2023-09-03 07:04:06 作者:慕凡

可能重复:   为什么你不能超载的WCF的方法?

我工作的一个项目中,我使用WCF服务。我的问题是,在WCF服务我有一个名为显示()的一种方法是客户端1我所用。现在我想用相同的名字,但有一个参数,即添加另一种方法。 显示(字符串名称),让新clinet2可以使用新的方法和旧客户端1可以使用旧的方法。我怎样才能做到这一点。这里是我写的code。 10Q提前。

 命名空间ContractVersioningService
{
  [的ServiceContract]
  公共接口IService1
  {
    [OperationContract的]
    字符串显示();

    [OperationContract的]
    字符串晚安();
  }
}

命名空间ContractVersioningService
{
   公共类服务1:IService1
   {
     公共字符串显示()
     {
        返回早上好;
     }

     公共字符串晚安()
     {
        返回晚安;
     }
   }
}

命名空间ContractVersioningService
{
  [的ServiceContract(命名空间=ContractVersioningService / 01,名称=ServiceVersioning)
  公共接口IService2:IService1
  {
     [OperationContract的]
     字符串显⽰(串迎接);
  }
}

命名空间ContractVersioningService
{

   公共类服务2:服务1,IService2
   {
      公共字符串显示(字符串名称)
      {
         返回名称;
      }

      公共字符串显⽰(字符串s)
      {
         返回S;
      }
   }
}
 

解决方案

 为什么WCF亘古不支持直接的方法重载?
 

如何规避录入重复房客源,这个步骤越早知道越好

由于WSDL亘古不支持方法的重载(不OOPS)。 WCF产生的WSDL用于指定的服务和操作或方法的服务公开的位置。

WCF使用文档/文字的WSDL风格:微软提出这个标准,其中SOAP主体元素将包含Web方法的名称

在默认情况下所有的WCF服务符合文档文字标准,其中皂体应包括方法名。

的唯一方法是使用name属性。对于如

  [OperationContract的(名称=整数)
    INT显示(INT A,INT B)
    [OperationContract的(名称为双打)
    双显示(双A,双B)
 

该编译器会生成以下,这是有道理的,WSDL定位

  [系统。codeDom.Compiler.Generated codeAttribute(System.ServiceModel,3.0.0.0)]
    [System.ServiceModel.ServiceContractAttribute(ConfigurationName =
    ServiceRef.IService1)]
  公共接口IService1
   {
       [System.ServiceModel.OperationContractAttribute(
       行动=htt​​p://tempuri.org/Service1/AddNumber
       ReplyAction =htt​​p://tempuri.org/IHelloWorld/IntegersResponse)]
       INT显示(INT A,INT B)

       [System.ServiceModel.OperationContractAttribute(
       行动=htt​​p://tempuri.org/IHelloWorld/ConcatenateStrings
       ReplyAction =htt​​p://tempuri.org/Service1/DoublesResponse)]
       双显示(双A,双B)
  }
 

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 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. 10Q in advance.

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 ?

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