方法 : XML Web サービス クライアントによって要求される SOAP ヘッダーを処理する

このトピックの対象は、レガシ テクノロジに特定されています。XML Web サービスと XML Web サービス クライアントは以下を使用して作成してください。 Windows Communication Foundation.

コード例

クライアントは、Web サービス メソッドに対して、SOAP 要求が正常に処理されるように 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 によって処理されます。以降の手順で、2 つの事例を処理する方法について説明します。

Web サービスによって定義されていないが、Web サービス クライアントによって要求された SOAP ヘッダーを処理するには

  1. Web サービス クライアントからの不明な SOAP ヘッダーを処理する手順に従います。特に、SOAP ヘッダーの DidUnderstand プロパティには注意が必要です。

    Web サービスによって定義されていない SOAP ヘッダーでは、DidUnderstand の初期値は false です。Web サービス メソッドから制御が返された後で DidUnderstand プロパティに false が設定された SOAP ヘッダーを ASP.NET が検出した場合は、SoapHeaderException が自動的にスローされます。

Web サービス クライアントによって要求され、Web サービスで定義されている SOAP ヘッダーを処理するには

  1. ASP.NET を使用して作成された Web サービスの SOAP ヘッダーを各 Web サービス メソッド内で処理する手順に従います。

    Web サービスによって定義され、SOAP ヘッダーを受け取る Web サービス メソッドで処理される SOAP ヘッダーの場合、ASP.NET では、その Web サービスが SOAP ヘッダーを認識すると仮定して、DidUnderstand の初期値を true に設定します。

次の MyWebService Web サービスは、MyHeader SOAP ヘッダーを定義し、MyWebMethod Web サービス メソッドの呼び出し時にこの SOAP ヘッダーも一緒に送信するように要求します。また、MyWebMethod によって不明の SOAP ヘッダーが処理されます。MyWebMethod が処理できる SOAP ヘッダーの場合、DidUnderstandtrue に設定されます。

<%@ 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(ja-jp,VS.100).gif注 :
ASP.NET では、DidUnderstand プロパティを使用して Web サービス メソッドと通信します。このプロパティは SOAP 仕様の一部ではなく、その値は SOAP 要求または SOAP 応答には含まれません。

y4t36w86.note(ja-jp,VS.100).gif注 :
Web サービスが SOAP ヘッダーを転送する場合、特に Actor の値に関係するものについては、SOAP 仕様の規則に従うことが重要です。詳細については、W3C の Web サイト (http://www.w3.org/TR/SOAP) を参照してください。

参照

リファレンス

SoapHeader
SoapHeaderAttribute
SoapUnknownHeader
SoapHeaderException

概念

XML Web サービス クライアントの作成

その他のリソース

SOAP ヘッダーの使用
ASP.NET を使用した XML Web サービス