Procedura: definire ed elaborare le intestazioni SOAP

Questo argomento è specifico di una tecnologia legacy. Servizi Web XML e client di servizi Web XML devono essere creati attualmente tramite Windows Communication Foundation.

Esempio di codice

I servizi creati tramite ASP.NET possono definire e manipolare le intestazioni SOAP. La definizione di un'intestazione SOAP viene effettuata definendo una classe che rappresenta i dati in una particolare intestazione SOAP e derivandoli dalla classe SoapHeader.

Per definire una classe che rappresenta un'intestazione SOAP

  1. Creare una classe che deriva dalla classe SoapHeader con un nome che corrisponda all'elemento radice per l'intestazione SOAP.

    public class MyHeader : SoapHeader
    
    Public Class MyHeader : Inherits SoapHeader
    
  2. Aggiungere campi o proprietà pubbliche, che corrispondano ai nomi e ai rispettivi tipi di dati per ogni elemento dell'intestazione SOAP.

    Ad esempio, per l'intestazione SOAP seguente, la classe proposta definisce una classe che rappresenta l'intestazione SOAP.

    <soap:Header xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/">
      <MyHeader xmlns="https://www.contoso.com">
        <Created>dateTime</Expires>
        <Expires>long</Expires>
      </MyHeader>
    </soap:Header>
    
    public class MyHeader : SoapHeader 
    {
       public DateTime Created;
       public long Expires;
    }
    
    Public Class MyHeader : Inherits SoapHeader 
       Public Created As DateTime
       Public Expires As Long
    End Class
    

Per elaborare intestazioni SOAP all'interno di un servizio Web

  1. Aggiungere un membro pubblico alla classe che implementa il servizio Web del tipo che rappresenta l'intestazione SOAP.

    [WebService(Namespace="https://www.contoso.com")]
    public class MyWebService 
    {
        // Add a member variable of the type deriving from SoapHeader.
        public MyHeader timeStamp;
    
    <WebService(Namespace:="https://www.contoso.com")> _
    Public Class MyWebService
        ' Add a member variable of the type deriving from SoapHeader.
        Public TimeStamp As MyHeader
    
  2. Applicare un attributo SoapHeader a ogni metodo del servizio Web creato per elaborare l'intestazione SOAP. Impostare la proprietà MemberName dell'attributo SoapHeader sul nome della variabile membro creata nel primo passaggio.

        [WebMethod]
        [SoapHeader("timeStamp")]
        public void MyWebMethod()
    
        <WebMethod, SoapHeader("TimeStamp")> _ 
        Public Sub MyWebMethod()
    
  3. All'interno di ogni metodo del servizio Web al quale l'attributo SoapHeader è stato applicato, accedere alla variabile membro creata nel primo passaggio per elaborare i dati inviati nell'intestazione SOAP.

        [WebMethod]
        [SoapHeader("myHeaderMemberVariable")]
        public string MyWebMethod() 
        {
            // Verify that the client sent the SOAP Header.
            if (timeStamp == null) timeStamp = new MyHeader();
            // Set the value of the SoapHeader returned to the client.
            timeStamp.Expires = 60000;
            timeStamp.Created = DateTime.UtcNow;
    
            return("Hello World!");
        }
    
        <WebMethod,SoapHeader("TimeStamp", _
                              Direction:=SoapHeaderDirection.InOut)> _ 
        Public Function MyWebMethod() As String
            ' Process the SoapHeader.
            If (TimeStamp Is Nothing) Then
                TimeStamp = New MyHeader
            End If
            TimeStamp.Expires = 60000
            TimeStamp.Created = DateTime.UtcNow
    
            Return "Hello World!"
        End Function
    

Esempio

Nel seguente esempio di codice viene illustrato come definire ed elaborare un'intestazione SOAP in un servizio Web creato utilizzando ASP.NET. Il servizio Web MyWebService ha una variabile membro denominata myHeaderMemberVariableche è di un tipo che deriva da SoapHeader (MyHeader) e è impostata sulla proprietà MemberName dell'attributo SoapHeader. Inoltre, un attributo SoapHeader viene applicato al metodo del servizio Web MyWebMethod che specifica myHeaderMemberVariable. All'interno del metodo del Servizio Web viene effettuato l'accesso a MyWebMethod, myHeaderMemberVariable per ottenere il valore dell'elemento XML Username dell'intestazione SOAP.

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

// Define a SOAP header by deriving from the SoapHeader class.
public class MyHeader : SoapHeader 
{
   public DateTime Created;
   public long Expires;
}

[WebService(Namespace="https://www.contoso.com")]
public class MyWebService 
{
    // Add a member variable of the type deriving from SoapHeader.
    public MyHeader myHeaderMemberVariable;
 
    // Apply a SoapHeader attribute.
    [WebMethod]
    [SoapHeader("myHeaderMemberVariable")]
    public void MyWebMethod() 
    {
        // Process the SoapHeader.
        if (myHeaderMemberVariable.Username == "admin")
        {
           // Do something interesting.
        }
    }
}
<%@ 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 Username As String
    Public Password As String
End Class

<WebService(Namespace:="https://www.contoso.com")> _
Public Class MyWebService 
    ' Add a member variable of the type deriving from SoapHeader.
    Public myHeaderMemberVariable As MyHeader
 
    ' Apply a SoapHeader attribute.
    <WebMethod, SoapHeader("myHeaderMemberVariable")> _
    Public Sub MyWebMethod()
        ' Process the SoapHeader.
        If (myHeaderMemberVariable.Username = "admin") Then
           ' Do something interesting.
        End If
    End Sub
End Class

Nell'esempio precedente, se la richiesta SOAP a MyWebMethod ha un'intestazione SOAP MyHeader con un elemento UserName impostato su Admin, viene eseguito codice aggiuntivo. Ovvero, la richiesta SOAP seguente causa l'esecuzione del codice.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <MyHeader xmlns="https://www.contoso.com">
      <Created>dateTime</Created>
      <Expires>long</Expires>
    </MyHeader>
  </soap:Header>
  <soap:Body>
    <MyWebMethod xmlns="https://www.contoso.com" />
  </soap:Body>
</soap:Envelope>

Vedere anche

Riferimento

SoapHeader
SoapHeaderAttribute
SoapUnknownHeader
SoapHeaderException

Concetti

Compilazione di client dei servizi Web XML

Altre risorse

Utilizzo delle intestazioni SOAP
Creare servizi Web XML mediante ASP.NET