Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在WCF中有两个同名的方法?_C#_.net_Wcf - Fatal编程技术网

C# 如何在WCF中有两个同名的方法?

C# 如何在WCF中有两个同名的方法?,c#,.net,wcf,C#,.net,Wcf,可能重复: 我正在从事一个使用WCF服务的项目。我的问题是,在WCF服务中,我有一个名为Display()的方法,由我的客户端1使用 现在我想添加另一个具有相同名称但只有一个参数的方法,即,Display(string name),这样新的clinet2可以使用新方法,旧的client1可以使用旧方法。我怎样才能做到这一点?这是我写的代码 namespace ContractVersioningService { [ServiceContract] public interface I

可能重复:

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

现在我想添加另一个具有相同名称但只有一个参数的方法,即,
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;
      }
   }
}

好吧,我要给你一个答案,因为现在评论太多了

您基本上有两种选择:

  • 使用单个接口(请注意,正如您在问题中所建议的,接口继承在技术上被视为一个接口),但是您必须为每个服务操作指定一个不同的名称。您可以通过将C#methods命名为distinct,或者通过应用
    [OperationContract(Name=“distinctname”)]
    属性来实现这一点

  • 使用两个独立的接口,它们之间没有任何继承关系,在不同的端点上发布每个接口。然后,您可以在每个中都有一个服务操作,名称相同,但参数不同。当然,如果愿意/需要的话,您仍然可以用一个实现类实现两个接口

  • 因为WSDL不支持方法重载(不是OOPs)。 WCF生成WSDL,指定服务的位置以及服务公开的操作或方法

    WCF使用文档/文字WSDL样式:Microsoft提出了此标准,其中soap主体元素将包含web方法名称

  • 默认情况下,所有WCF服务都符合document literal标准,其中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)
  }

@克里斯蒂安根据您的链接,我们需要为运营合同提供“名称”属性。但问题是,客户端随后可以使用属性“name”值访问这两种方法。我希望客户端可以通过原始名称访问这些方法。@Sagar客户端将看到这些方法的名称,而不是您在C#界面中所命名的名称,而是通过
OperationContract.name
值中给出的名称。这就是问题的关键。实际上,该方法不会有相同的名称。这是唯一可行的办法。您不能有多个名称完全相同的服务操作—WSDL不会执行重载解析。因此,这是一个重复的链接问题。@Christian.K,但我希望客户端只看到一个通用名称。我如何实现它?WCF不支持方法重载,例如,不能有两个同名的方法,而只是有不同的参数列表。服务描述中的名称必须是唯一的。没有办法绕过这个要求。但是我必须从两个接口显式地实现所有的方法。是的,但是这是一个问题吗?无论如何你都得这么做。是的,是。。。。它将不必要地增加代码。然后委派。一个函数(旧函数)可以调用新函数,为新/额外参数提供合理的默认值。除此之外,恐怕我看不出你的问题。无论是否使用WCF,这看起来都是一件常规/平常的事情。您可能应该考虑发布一个新问题或更新当前的问题,然后调用方法将使用“InutsSo”和“Douple()”。我希望在没有name属性的单个servicecontract中通过原始名称(即Display)方法重载用户方法是不可能的。但您可以将其公开为两个不同的契约。可以使用name属性按原始(显示)名称调用。我认为您只在服务器端更新name属性。它必须位于两侧。是否可以从服务器端更改clients reference.cs文件?否。如何创建代理(serviceutil/Channelfactory)?如果仅在服务器端实现name属性,则只能在客户端获取别名。。所以两边都更新,你就得到了原来的名字。。这可能会有帮助
     [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)
  }