WCF-自定义响应消息

问题描述:

我得到了这样的XSD以进行服务响应:

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>

请注意,响应消息中唯一的元素名称是"response".

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);
}

我将XmlElement属性应用于AddEdit操作的返回值,但是我仍然得到以下XSD:

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>

无论[return:XmlElement]属性中的名称如何,AddEditResponse元素内的元素名称均保持不变.

The name of the element inside the AddEditResponse element stays the same regardless of name in [return:XmlElement] attribute.

为什么会这样?有什么方法可以在WCF中自定义此类数据交换格式的详细信息?

Why is this happening? Is there any way to customize such details of data exchange format in WCF?

谢谢.

我将通过让svcutil为我构建合同来解决这个问题.看看是否有帮助...

I would approach this by having svcutil build the contract for me. See if it helps...

使用您必须使用的模式来生成至少一个与您的AddEdit方法相匹配的操作的WSDL文件.

Use the schema you have to generate a WSDL file with one operation at least, that matches your AddEdit method.

一旦有了,请使用类似的命令行运行svcutil(在我的情况下,我使用的工具会生成三个WSDL文件,其中一个引用XSD文件):

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):

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

看看生成的代码以找到问题的答案(可能还有更多).

Take a look at the generated code to find the answer to your question (and maybe more).

对于此XSD(以请求/响应对为例):

For this XSD (showing the request/response pair as an example):

<?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);
}