如何:控制 Web 服务方法参数是否包含在额外的元素中

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

可以将 Web 服务方法的参数或返回值自动包装在 SOAP 消息的 Body 元素内的父 XML 元素中,或者直接绑定到 Web 服务描述语言 (WSDL) 文档中的消息 part 元素中。.NET Framework 分别以包装消息和裸消息的形式引用这两个选项,并使用特性来控制它们。

指定将参数封装在一个 XML 元素中

  1. SoapDocumentMethod 特性应用于调用适当 Web 服务方法的代理类中的方法,并将 ParameterStyle 属性设置为 Wrapped

    下面的代码示例将 ParameterStyle 设置为 Wrapped。此外,它还将参数格式设置样式设置为 Literal

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

    SOAP 请求的 XML 部分将参数封装在默认以 Web 服务方法命名的元素中。

    <?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>
        <DocumentWrappedLiteral xmlns="https://www.contoso.com">
          <MyAddress>
            <Street>string</Street>
            <City>string</City>
            <Zip>string</Zip>
          </MyAddress>
          <useZipPlus4>boolean</useZipPlus4>
        </DocumentWrappedLiteral>
      </soap:Body>
    </soap:Envelope>
    

    SOAP 响应的 XML 部分封装 Web 服务方法的 out 参数,包括元素中的结果。默认情况下,该包装元素的名称是追加了 Response 的 Web 服务方法的名称。

    <?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>
        <DocumentWrappedLiteralResponse xmlns="https://www.contoso.com">
          <DocumentWrappedLiteralResult>string
          </DocumentWrappedLiteralResult>
        </DocumentWrappedLiteralResponse>
      </soap:Body>
    </soap:Envelope>
    

指定参数直接跟在 Body 元素后面

  1. SoapDocumentMethod 特性应用于调用适当 Web 服务方法的代理类中的方法,并将 ParameterStyle 属性设置为 Bare

    下面的示例由 Wsdl.exe 生成,它将 ParameterStyle 设置为 Bare,并将参数格式设置样式设置为 Literal。由于命名空间不能在封装所有参数的元素中指定,因此必须为每个参数和返回值单独指定命名空间。通过向每个参数和返回值应用 XmlElementAttribute,并设置 Namespace 属性,可以实现此目的。

    [SoapDocumentMethod(
         "https://www.contoso.com/DocumentBareLiteral",
         Use=SoapBindingUse.Literal,
         ParameterStyle=SoapParameterStyle.Bare)]
    [return: XmlElement(Namespace="https://www.contoso.com",                    IsNullable=true)]
    public string DocumentBareLiteral(
       [XmlElement(Namespace="https://www.contoso.com",
                         IsNullable=true)] 
       Address1 MyAddress, 
       [XmlElement(Namespace="https://www.contoso.com",
                IsNullable=false)] 
       bool useZipPlus4) {
    
    <SoapDocumentMethod( _
         https://www.contoso.com/DocumentBareLiteral", _
         Use:=SoapBindingUse.Literal, _
         ParameterStyle:= SoapParameterStyle.Bare)> _
    Public Function DocumentBareLiteral( _
       ByVal <XmlElement([Namespace]:="https://www.contoso.com", _
                          IsNullable:=true)> _
       MyAddress As Address1, _
       ByVal <XmlElement([Namespace]:="https://www.contoso.com", _
                          IsNullable:=false)> _
       useZipPlus4 As Boolean) _
       As <XmlElement([Namespace]:="https://www.contoso.com", _
                      IsNullable:=true)> _
       String
    

    在 SOAP 请求内,参数映射到的 XML 元素直接跟在 Body 元素之后,其中每个元素指定一个命名空间。

    <?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>
        <MyAddress xmlns="https://www.contoso.com">
          <Street>string</Street>
          <City>string</City>
          <Zip>string</Zip>
        </MyAddress>
        <useZipPlus4 xmlns="https://www.contoso.com">boolean</useZipPlus4>
      </soap:Body>
    </soap:Envelope>
    

    out 参数(包括返回值)映射到 SOAP 响应内跟在 Body 元素之后的 XML 元素。默认情况下,返回值元素的名称是带有 Result 后缀的 Web 服务方法名称。

    <?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>
        <DocumentBareLiteralResult xmlns="https://www.contoso.com">
           string</DocumentBareLiteralResult>
      </soap:Body>
    </soap:Envelope>
    

另请参见

参考

SoapDocumentMethodAttribute
SoapRpcMethodAttribute

其他资源

自定义 SOAP 消息的格式设置