如何:控制 Web 服务方法的参数和返回值的格式设置

本主题专门介绍一项旧有技术。现在应通过使用以下链接来创建 XML Web 服务和 XML Web 服务客户端: Windows Communication Foundation.

对于与方法的参数和返回值或用法相对应的 XML 元素的格式设置,Web 服务描述语言 (WSDL) 提供了两个选项:SOAP encoded 和 literal。.NET Framework 在代码中使用特性来控制这两个选项。虽然 ASP.NET 为了控制 XML 的格式设置方式提供了可扩展体系结构,但无法保证对参数进行序列化的顺序。

指定 Literal 参数格式设置

  1. SoapDocumentMethod 特性应用于代理类中的方法,同时将 Use 属性设置为 SoapBindingUse.Literal

    SoapBindingUse 枚举指定使用 ASP.NET 创建的 Web 服务可以使用的参数格式设置样式。

    [SoapDocumentMethod(
        "https://www.contoso.com/DocumentLiteral",
        RequestNamespace="https://www.contoso.com",
        ResponseNamespace="https://www.contoso.com",
        Use=SoapBindingUse.Literal)]
    public string DocumentLiteral(Address1 address, bool useZipPlus4) {
    
    <SoapDocumentMethod( _
       "https://www.contoso.com/DocumentLiteral", _
       RequestNamespace:="https://www.contoso.com", _
       ResponseNamespace:="https://www.contoso.com", _
       Use:=SoapBindingUse.Literal)>  _
    Public Function DocumentLiteral(ByVal address As Address1, _
                             ByVal useZipPlus4 As Boolean) As String
    

    DocumentLiteral Web 服务方法所遵循的 SOAP 请求的 XML 部分。参数驻留在 Body 元素内并编码为自包含的 XML 文档,因为它们引用 XSD 架构。

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <DocumentLiteral xmlns="https://www.contoso.com">
          <address>
            <Street>One Microsoft Way</Street>
            <City>Redmond</City>
            <Zip>98052</Zip>
          </address>
          <useZipPlus4>True</useZipPlus4>
        </DocumentLiteral>
      </soap:Body>
    </soap:Envelope>
    

指定 Encoded 参数格式设置

  1. SoapDocumentMethod 特性或 SoapRpcMethod 特性应用于代理类中的方法,同时将 Use 属性设置为 SoapBindingUse.Encoded

    Literal 参数格式设置样式不同,Encoded 参数格式设置样式与两种 Web 服务方法格式设置样式均可结合使用。有关 Web 服务方法格式设置样式的更多详细信息,请参见 .NET Framework 对 SOAP 格式的支持

    [SoapDocumentMethod("https://www.contoso.com/DocumentEncoded",
                        RequestNamespace="https://www.contoso.com",
                        ResponseNamespace="https://www.contoso.com",
                        Use=SoapBindingUse.Encoded)]
    public string DocumentEncoded(Address address, bool useZipPlus4) {
    
    <SoapDocumentMethod("https://www.contoso.com/DocumentEncoded", _
                        RequestNamespace:="https://www.contoso.com", _
                        ResponseNamespace:="https://www.contoso.com", _
                        Use:=SoapBindingUse.Encoded)>  _
    Public Function DocumentEncoded(ByVal address As Address, _ 
                      ByVal useZipPlus4 As Boolean) As String
    

    DocumentEncoded 服务方法所遵循的 SOAP 请求的 XML 部分。请注意,这些参数的表示方式与 Literal 格式设置样式存在很大的差异,因为这些参数是用 SOAP 规范第 5 节中概述的编码规则来设置格式的。请特别关注 address 参数,该参数不是简单数据类型。

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:soapenc="https://schemas.xmlsoap.org/soap/encoding/"
    xmlns:tns="https://www.contoso.com"
    xmlns:tnsTypes="https://www.contoso.com/encodedTypes"
    xmlns:wsdl="https://schemas.xmlsoap.org/wsdl/"
    xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body soap:encodingStyle="https://schemas.xmlsoap.org/soap/encoding/">
        <tnsTypes:DocumentEncoded>
          <address href="#1" />
          <useZipPlus4>boolean</useZipPlus4>
        </tnsTypes:DocumentEncoded>
        <tnsTypes:Address id="1">
          <Street id="2">string</Street>
          <City id="3">string</City>
          <Zip id="4">string</Zip>
        </tnsTypes:Address>
      </soap:Body>
    </soap:Envelope>
    

另请参见

参考

SoapDocumentMethodAttribute
SoapRpcMethodAttribute

其他资源

自定义 SOAP 消息的格式设置