WCF - 定制响应消息
问题描述:
我得到了这样的XSD的服务响应:WCF - 定制响应消息
<xs:element name="AddEditResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="1" name="response" type="xs:boolean"/>
</xs:sequence>
</xs:complexType>
</xs:element>
注意,只有元素的响应消息中的名称是“响应”。
[ServiceContract]
[XmlSerializerFormat]
public interface IService
{
[OperationContract]
[return:XmlElement("return")]
bool AddEdit(MultipleElements elements);
}
我申请的XmlElement属性AddEdit操作的返回值,但我仍然得到以下XSD:
<xs:element name="AddEditResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="1" name="AddEditResult" type="xs:boolean"/>
</xs:sequence>
</xs:complexType>
</xs:element>
的AddEditResponse元素中的元素的名称保持不变不管名称在[return:XmlElement]属性中。
这是怎么发生的?有什么方法可以自定义WCF中的数据交换格式的细节?
谢谢。
答
我会通过让svcutil为我建立合同来解决这个问题。看看它是否有帮助...
使用您必须通过一个操作生成WSDL文件的模式(至少与您的AddEdit方法相匹配)。
一旦你有一个,运行一个类似的命令行SvcUtil工具(在我的情况,我现在用的工具生成三个WSDL文件中的一个引用XSD文件):
svcutil /mc AddEditSoapHttp.wsdl AddEditSoapBinding.wsdl AddEditInterface.wsdl WCF-WSDLFirst.xsd
结果应该是像这样:
Microsoft (R) Service Model Metadata Tool
[Microsoft (R) Windows (R) Communication Foundation, Version 4.0.30319.1]
Copyright (c) Microsoft Corporation. All rights reserved.
Generating files...
....\AddEditSoapHttpService.cs
....\output.config
看看生成的代码来找到你的问题的答案(也许更多)。
对于此XSD(示出了请求/响应对作为例子):
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="AddEditRequest">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="1" name="request" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="AddEditResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="1" name="response" type="xs:boolean"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
我正在(我张贴的摘录只):
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace="http://tempuri.org/ifx/addedit/interface/1/", ConfigurationName="AddEditPortType")]
public interface AddEditPortType
{
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ifx/addedit/bindings/1/AddEdit", ReplyAction="*")]
[System.ServiceModel.XmlSerializerFormatAttribute()]
AddEditResponse1 AddEdit(AddEditRequest1 request);
}
谢谢,这是我通缉。我是新来的WCF,我不知道svcutil。但是,它生成消息合同,而不是数据合同。 “如果用于模拟消息的命名约定与WCF标准不同,代码生成工具将为请求和响应生成MessageContract类” - from [this](http://msdn.microsoft.com/en-us/杂志/ ee335699.aspx)文章。 –