【问题标题】:WCF - customize response messageWCF - 自定义响应消息
【发布时间】:2012-02-16 09:16:11
【问题描述】:

我收到了这样的 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>

请注意,响应消息中唯一元素的名称是“response”。

[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>

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

为什么会这样?有没有办法在 WCF 中自定义这些数据交换格式的细节?

谢谢。

【问题讨论】:

    标签: .net wcf xsd


    【解决方案1】:

    我会通过让 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 类”——来自this 文章。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-10
    • 2014-07-29
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    相关资源
    最近更新 更多