当我使用 Visual Studio 创建 WCF 客户端(“添加服务引用”)并单击“高级...”时,显示“始终生成消息协定”的复选框确实可以正确控制消息协定对象是否为生成。
这是不正确的。尝试使用链接中存在问题的 WSDL,您将获得与使用 ServiceContractGenerator 相同的结果。事实上,ServiceContractGenerationOptions.TypedMessages 标志(默认关闭)直接对应于前面提到的对话框选项,并用于(打开时)强制创建消息合约。
话虽如此,问题出在 WSDL 中,并在生成的 .cs 文件中用如下行指示:
// CODEGEN:生成消息契约,因为来自命名空间http://localhost/FinSwitch/ 的元素名称登录未标记为可空
这就是问题所在。当方法元素或响应元素包含未用nillable="true" 标记的“对象类型”(字符串、base64Binary 等)成员时,svcutil.exe、“添加服务引用”对话框和ServiceContractGenerator 都不会解开方法。
例如,这是有问题的 WSDL 的一部分:
<s:element name="DownloadFile">
<s:complexType>
<s:sequence>
<s:element type="s:string" name="login" maxOccurs="1" minOccurs="0"/>
<s:element type="s:string" name="password" maxOccurs="1" minOccurs="0"/>
<s:element type="s:string" name="fileType" maxOccurs="1" minOccurs="0"/>
<s:element type="s:dateTime" name="fileDate" maxOccurs="1" minOccurs="1"/>
<s:element type="s:boolean" name="onlyDownloadIfFileChanged" maxOccurs="1" minOccurs="1"/>
<s:element type="s:string" name="companyCode" maxOccurs="1" minOccurs="0"/>
<s:element type="s:string" name="category" maxOccurs="1" minOccurs="0"/>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="DownloadFileResponse">
<s:complexType>
<s:sequence>
<s:element type="s:base64Binary" name="DownloadFileResult" maxOccurs="1" minOccurs="0"/>
</s:sequence>
</s:complexType>
</s:element>
生成
// CODEGEN: Generating message contract since element name login from namespace http://localhost/FinSwitch/ is not marked nillable
[System.ServiceModel.OperationContractAttribute(Action="http://localhost/FinSwitch/FinSwitchWebServiceSoap/DownloadFileRequest", ReplyAction="http://localhost/FinSwitch/FinSwitchWebServiceSoap/DownloadFileResponse")]
DownloadFileResponse DownloadFile(DownloadFileRequest request);
加消息联系类。
但是,如果我们将其修改为:
<s:element name="DownloadFile">
<s:complexType>
<s:sequence>
<s:element type="s:string" name="login" nillable="true" maxOccurs="1" minOccurs="0"/>
<s:element type="s:string" name="password" nillable="true" maxOccurs="1" minOccurs="0"/>
<s:element type="s:string" name="fileType" nillable="true" maxOccurs="1" minOccurs="0"/>
<s:element type="s:dateTime" name="fileDate" maxOccurs="1" minOccurs="1"/>
<s:element type="s:boolean" name="onlyDownloadIfFileChanged" maxOccurs="1" minOccurs="1"/>
<s:element type="s:string" name="companyCode" nillable="true" maxOccurs="1" minOccurs="0"/>
<s:element type="s:string" name="category" nillable="true" maxOccurs="1" minOccurs="0"/>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="DownloadFileResponse">
<s:complexType>
<s:sequence>
<s:element type="s:base64Binary" name="DownloadFileResult" nillable="true" maxOccurs="1" minOccurs="0"/>
</s:sequence>
</s:complexType>
</s:element>
那么生成的代码和预期的一样
[System.ServiceModel.OperationContractAttribute(Action="http://localhost/FinSwitch/FinSwitchWebServiceSoap/DownloadFileRequest", ReplyAction="http://localhost/FinSwitch/FinSwitchWebServiceSoap/DownloadFileResponse")]
byte[] DownloadFile(string login, string password, string fileType, System.DateTime fileDate, bool onlyDownloadIfFileChanged, string companyCode, string category);
并且没有消息契约类。
这一切是什么意思?该规则在基础架构中进行了深入的硬编码(如果有人感兴趣,here 是参考源)并且无法更改。可以预处理 WSDL 内容(毕竟,它是一个 XML)并在需要的地方插入 nillable="true",但我不确定这是否可以被认为是正确的操作 - AFAIK,提供正确的 WSDL 是服务提供商的责任和不能保证改变它不会引起副作用。