WCF-自定义响应消息
我收到了这样的 XSD 服务响应:
<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中自定义数据交换格式的此类细节?
谢谢。
I was given such XSD for a service response:
<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>
Notice that the name of the only element in response message is "response".
[ServiceContract]
[XmlSerializerFormat]
public interface IService
{
[OperationContract]
[return:XmlElement("return")]
bool AddEdit(MultipleElements elements);
}
I applied XmlElement attribute to the return value of AddEdit operation, but I'm still getting the following 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>
The name of the element inside the AddEditResponse element stays the same regardless of name in [return:XmlElement] attribute.
Why is this happening? Is there any way to customize such details of data exchange format in WCF?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会通过让 svcutil 为我构建合同来解决这个问题。看看它是否有帮助...
使用您必须的架构至少通过一项操作生成 WSDL 文件,该文件与您的 AddEdit 方法相匹配。
完成后,使用类似的命令行运行 svcutil(在我的例子中,我使用的工具生成三个 WSDL 文件,其中一个引用 XSD 文件):
结果应该是这样的:
看一下生成的代码找到您问题的答案(也许还有更多)。
对于此 XSD(以请求/响应对为例):
我得到(我仅发布摘录):
I would approach this by having svcutil build the contract for me. See if it helps...
Use the schema you have to generate a WSDL file with one operation at least, that matches your AddEdit method.
Once you have that, run the svcutil with a similar command line (in my case, the tool I am using generates three WSDL files one referencing the XSD file):
The result should be something like this:
Take a look at the generated code to find the answer to your question (and maybe more).
For this XSD (showing the request/response pair as an example):
I am getting (I am posting an excerpt only):