共用方式為


HOW TO:處理 XML Web Service 用戶端所需的 SOAP 標頭

本主題專門說明舊有技術。 應該使用下列建立 XML Web Service 及 XML Web Service 用戶端: Windows Communication Foundation.

程式碼範例

用戶端可能需要 Web 服務方法正確解譯 SOAP 標頭的語意 (Semantics) 並適當加以處理,SOAP 要求才會成功。若要執行這項操作,用戶端會將 SOAP 標頭的 mustUnderstand 屬性設為 1。例如,下列 SOAP 要求會指示 SOAP 要求收件者必須處理 MyCustomSoapHeader SOAP 標頭。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/" >
  <soap:Header>
    <MyCustomSoapHeader soap:mustUnderstand="1" xmlns="https://www.contoso.com">
      <custom>Keith</custom>
    </MyCustomSoapHeader>
  </soap:Header>
  <soap:Body>
    <MyUnknownHeaders xmlns="https://www.contoso.com" />
  </soap:Body>
</soap:Envelope>

Web 服務是否定義 SOAP 標頭可以決定 Web 服務應如何處理用戶端所要求的 SOAP 標頭。在 Web 服務定義 SOAP 標頭的情況下,有許多工作會由 ASP.NET 加以處理。在接下來的程序中,您可以學到處理兩種情況的方式。

若要處理 Web 服務未定義、但 Web 服務用戶端已要求的 SOAP 標頭

  1. 依照處理來自 Web 服務用戶端之未知 SOAP 標頭的步驟執行,並特別注意 SOAP 標頭的 DidUnderstand 屬性。

    對不是由 Web 服務定義的 SOAP 標頭來說,DidUnderstand 的初始值是 false。如果在 Web 服務方法回傳之後,ASP.NET 偵測到某個 SOAP 標頭,其 DidUnderstand 屬性是設定為 false,則會自動擲回 SoapHeaderException

若要處理 Web 服務用戶端所要求的、且由 Web 服務定義的 SOAP 標頭

  1. 在每個 Web 服務方法中,依照在 Web 服務 (使用 ASP.NET 所建立) 中處理 SOAP 標頭的步驟執行。

    對於 Web 服務所定義而在 Web 服務方法 (此方法接收 SOAP 標頭) 中處理的 SOAP 標頭,ASP.NET 會假設 Web 服務了解 SOAP 標頭,並且將 DidUnderstand 的初始值設定為 true

範例

下列 MyWebService Web 服務會定義 MyHeader SOAP 標頭,並會要求使用 MyWebMethod Web 服務方法的任何呼叫來傳送。此外,MyWebMethod 還會處理任何未知的 SOAP 標頭。對於 MyWebMethod 所能處理的 SOAP 標頭,會將 DidUnderstand 設定為 true

<%@ WebService Language="C#" Class="MyWebService" %>
using System.Web.Services;
using System.Web.Services.Protocols;

// Define a SOAP header by deriving from the SoapHeader base class.
public class MyHeader : SoapHeader {
    public string MyValue;
}
public class MyWebService {
    public MyHeader myHeader;

    // Receive all SOAP headers other than the MyHeader SOAP header.
    public SoapUnknownHeader[] unknownHeaders;
 
    [WebMethod]
    [SoapHeader("myHeader")]
    //Receive any SOAP headers other than MyHeader.
    [SoapHeader("unknownHeaders")]
    public string MyWebMethod() 
    {
       foreach (SoapUnknownHeader header in unknownHeaders) 
       {
         // Perform some processing on the header.
         if (header.Element.Name == "MyKnownHeader")
               header.DidUnderstand = true;
         else
                // For those headers that cannot be 
                // processed, set DidUnderstand to false.
                header.DidUnderstand = false;
       }
       return "Hello";
    }
}
<%@ WebService Language="VB" Class="MyWebService" %>
Imports System.Web.Services
Imports System.Web.Services.Protocols

' Define a SOAP header by deriving from the SoapHeader base class.
Public Class MyHeader : Inherits SoapHeader
    Public MyValue As String
End Class

Public Class MyWebService
    Public myHeader As MyHeader
    
    ' Receive all SOAP headers other than the MyHeader SOAP header.
    Public unknownHeaders() As SoapUnknownHeader    
    
    <WebMethod, _
     SoapHeader("myHeader"), _
     SoapHeader("unknownHeaders")> _
    Public Function MyWebMethod() As String
        'Receive any SOAP headers other than MyHeader.
        Dim header As SoapUnknownHeader        For Each header In unknownHeaders
            ' Perform some processing on the header.
            If header.Element.Name = "MyKnownHeader" Then
                header.DidUnderstand = True
            ' For those headers that cannot be 
            ' processed, set DidUnderstand to false.
            Else
                header.DidUnderstand = False
            End If
        Next header
        Return "Hello"
    End Function
End Class
y4t36w86.note(zh-tw,VS.100).gif注意:
ASP.NET 會使用 DidUnderstand 屬性,與 Web 服務方法通訊。它不是 SOAP 規格的一部分,它的值不會出現在 SOAP 要求或回應的任何部分。

y4t36w86.note(zh-tw,VS.100).gif注意:
如果 Web 服務會轉送 SOAP 標頭,那麼遵循 SOAP 規格中的規則就非常重要,特別是那些有關 Actor 值的規則。如需詳細資訊,請參閱 W3C 網站 (http://www.w3.org/TR/SOAP/)。

另請參閱

參考

SoapHeader
SoapHeaderAttribute
SoapUnknownHeader
SoapHeaderException

概念

建置 XML Web Service 用戶端

其他資源

使用 SOAP 標頭
使用 ASP.NET 的 XML Web Service