Export (0) Print
Expand All

SoapHeader Class

When overridden in a derived class, represents the content of a SOAP header.

For a list of all members of this type, see SoapHeader Members.

System.Object
   System.Web.Services.Protocols.SoapHeader
      System.Web.Services.Protocols.SoapUnknownHeader

[Visual Basic]
MustInherit Public Class SoapHeader
[C#]
public abstract class SoapHeader
[C++]
public __gc __abstract class SoapHeader
[JScript]
public abstract class SoapHeader

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

SOAP headers offer a method for passing data to and from an XML Web service method if the data is not directly related to the XML Web service method's primary functionality. For instance, an XML Web service might contain several XML Web service methods that each require a custom authentication scheme. Instead of adding parameters to each XML Web service method for the custom authentication scheme, a SoapHeaderAttribute, referring to a class deriving from SoapHeader, can be applied to each XML Web service method. The implementation for the class deriving from SoapHeader handles the custom authentication scheme. In this manner, the XML Web service method implements only the functionality specific to it and adds additional functionality using a SOAP header.

The following list outlines the basic steps to receiving and processing a SOAP header:

  1. Create a class deriving from SoapHeader representing the data passed into the SOAP header.
  2. Add a member to the class implementing an XML Web service or an XML Web service client proxy class, of the type created in the first step.
  3. Apply a SoapHeaderAttribute to the XML Web service method or the corresponding method in the proxy class, specifying the member created in the second step in the MemberName property.
  4. Within the XML Web service method or XML Web service client code access the MemberName property to process the data sent in the SOAP header.

Example

[Visual Basic, C#] The following MyWebService XML Web service defines the MyHeader SOAP header and requires it to be sent with any calls to the MyWebMethod XML Web service method. Additionally, MyWebMethod receives any SOAP headers other than the MyHeader SOAP header.

[Visual Basic] 
<%@ WebService Language="VB" Class="MyWebService"%>

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Xml
Imports System

' 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 theHeader As MyHeader
    ' Receive all SOAP headers besides the MyHeader SOAP header.
    Public unknownHeaders() As SoapUnknownHeader    

    'Receive any SOAP headers other than MyHeader.    
    <WebMethod, _
     SoapHeader("theHeader", Direction := SoapHeaderDirection.InOut), _
     SoapHeader("unknownHeaders")> _
    Public Function MyWebMethod() As String
                
        Dim header As SoapUnknownHeader
        For Each header In unknownHeaders
            ' Perform some processing on the header.
            If header.Element.Name = "MyKnownHeader" Then
                header.DidUnderstand = True
            Else
                ' For those headers that cannot be
                ' processed, set the DidUnderstand propert to false.
                header.DidUnderstand = False
            End If
        Next header
        Return "Hello"
    End Function
    
End Class


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

// 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 besides the MyHeader SOAP header.
    public SoapUnknownHeader[] unknownHeaders;
 
    [WebMethod]
    [SoapHeader("myHeader", Direction=SoapHeaderDirection.InOut)]

    //Receive any SOAP headers other than MyHeader.
    [SoapHeader("unknownHeaders",Required=false)]

    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 the DidUnderstand property to false.
                header.DidUnderstand = false;
       }
       return "Hello";
    }
}

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.Services.Protocols

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Web.Services (in System.Web.Services.dll)

See Also

SoapHeader Members | System.Web.Services.Protocols Namespace | SoapUnknownHeader | SoapHeaderException | SoapHeaderAttribute | SoapHeaderDirection

Show:
© 2014 Microsoft