先由问题入手:
在服务契约里面的代码如下:
[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的值。
原文:http://www.cnblogs.com/zychengzhiit1/p/4358574.html