首页 > 其他 > 详细

WCF中的服务契约重载的问题

时间:2015-03-23 01:44:01      阅读:158      评论:0      收藏:0      [点我收藏+]

先由问题入手:

技术分享

在服务契约里面的代码如下:

   [OperationContract]
          string Hello(string name);
          [OperationContract]
          void Hello();

这里出现了Hello方法的重载,可是在WCF中,是不认可这样做的。

你也可以从wsdl中的operation节点看到;

<wsdl:operation name="HelloString">
  <wsdl:input wsaw:Action="http://tempuri.org/IHelloService/HelloString" message="tns:IHelloService_HelloString_InputMessage" /> 
  <wsdl:output wsaw:Action="http://tempuri.org/IHelloService/HelloStringResponse" message="tns:IHelloService_HelloString_OutputMessage" /> 
  </wsdl:operation>
- <wsdl:operation name="HelloVoid">
  <wsdl:input wsaw:Action="http://tempuri.org/IHelloService/HelloVoid" message="tns:IHelloService_HelloVoid_InputMessage" /> 
  <wsdl:output wsaw:Action="http://tempuri.org/IHelloService/HelloVoidResponse" message="tns:IHelloService_HelloVoid_OutputMessage" /> 
  </wsdl:operation>

大家看xml中的第一行 operation后面的name,它表示的就是在定义服务契约中的方法的名称;

如果我们像刚才上面的Hello方法那样去实现重载的话,那么就算解决方案编译成功了,wcf服务也启动了,可wsdl中会有两个name=“Hello”的operation节点;

<wsdl:operation name="Hello">
  <wsdl:input wsaw:Action="http://tempuri.org/IHelloService/HelloString" message="tns:IHelloService_HelloString_InputMessage" /> 
  <wsdl:output wsaw:Action="http://tempuri.org/IHelloService/HelloStringResponse" message="tns:IHelloService_HelloString_OutputMessage" /> 
  </wsdl:operation>
- <wsdl:operation name="Hello">
  <wsdl:input wsaw:Action="http://tempuri.org/IHelloService/HelloVoid" message="tns:IHelloService_HelloVoid_InputMessage" /> 
  <wsdl:output wsaw:Action="http://tempuri.org/IHelloService/HelloVoidResponse" message="tns:IHelloService_HelloVoid_OutputMessage" /> 
  </wsdl:operation>

出现两个Hello,那么怎么知道,我要调用哪个Hello尼;

 

所以,大家不能像过去C#代码编写,

在WCF中要想实现服务契约方法的重载,需要将方法重新起“别名”

   [OperationContract(Name="HelloString")]
          string Hello(string name);
          [OperationContract(Name="HelloVoid")]
          void Hello();

在OperationContract后面添加Name值,

添加之后,编译通过生成的wsdl:

<wsdl:operation name="HelloString">
  <wsdl:input wsaw:Action="http://tempuri.org/IHelloService/HelloString" message="tns:IHelloService_HelloString_InputMessage" /> 
  <wsdl:output wsaw:Action="http://tempuri.org/IHelloService/HelloStringResponse" message="tns:IHelloService_HelloString_OutputMessage" /> 
  </wsdl:operation>
- <wsdl:operation name="HelloVoid">
  <wsdl:input wsaw:Action="http://tempuri.org/IHelloService/HelloVoid" message="tns:IHelloService_HelloVoid_InputMessage" /> 
  <wsdl:output wsaw:Action="http://tempuri.org/IHelloService/HelloVoidResponse" message="tns:IHelloService_HelloVoid_OutputMessage" /> 
  </wsdl:operation>

大家需要注意这里,你在Name里面输入的字符串,编译后,会在wsdl文件中被当作operation节点的name的值。

 

WCF中的服务契约重载的问题

原文:http://www.cnblogs.com/zychengzhiit1/p/4358574.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!