HOW TO:控制額外項目中是否要包含 Web 服務方法參數

Web 服務方法的參數或傳回值可以自動封裝在 SOAP 訊息 Body 項目的父 XML 項目中,或直接繫結至 Web 服務描述語言 (WSDL) 文件中的訊息 part 項目。.NET Framework 將這兩個選擇分別稱為包裝和不包裝,並且會使用屬性來控制它們。

若要指定參數封裝在一個 XML 項目中

  • SoapDocumentMethod 屬性 (Attribute) 套用至 Proxy 類別中呼叫適用 Web 服務方法的方法,並且將 ParameterStyle 屬性 (Property) 設為 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 參數,包括項目內的結果。根據預設,封裝項目的名稱是 Web 服務方法的名稱附加 Response

    <?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 項目後面

  • SoapDocumentMethod 屬性 (Attribute) 套用至 Proxy 類別中呼叫適用 Web 服務方法的方法,並且將 ParameterStyle 屬性 (Property) 設為 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 項目。根據預設,傳回值項目名稱是 Web 服務方法的名稱加上 Result 後置字元。

    <?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 訊息格式

Footer image

Copyright © 2007 by Microsoft Corporation. All rights reserved.