使用 ASP.NET 创建的 Web 服务可以定义和处理 SOAP 头。定义 SOAP 头的过程是:定义表示特定 SOAP 头中数据的类,然后从 SoapHeader 类中派生该类。
定义表示 SOAP 头的类
-
创建一个从 SoapHeader 类派生的类,其名称与 SOAP 头的根元素匹配。
C#public class MyHeader : SoapHeader
Visual BasicPublic Class MyHeader : Inherits SoapHeader
-
添加公共字段或属性,与 SOAP 头中每个元素的名称及其各自的数据类型匹配。
例如,在给定以下 SOAP 头的情况下,其后的类定义一个表示 SOAP 头的类。
C#<soap:Header xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <MyHeader xmlns="http://www.contoso.com"> <Created>dateTime</Expires> <Expires>long</Expires> </MyHeader> </soap:Header> public class MyHeader : SoapHeader { public DateTime Created; public long Expires; }
Visual BasicPublic Class MyHeader : Inherits SoapHeader Public Created As DateTime Public Expires As Long End Class
在 Web 服务中处理 SOAP 头
-
将公共成员添加到实现表示 SOAP 头的类型的 Web 服务的类。
C#[WebService(Namespace="http://www.contoso.com")] public class MyWebService { // Add a member variable of the type deriving from SoapHeader. public MyHeader timeStamp;
Visual Basic<WebService(Namespace:="http://www.contoso.com")> _ Public Class MyWebService ' Add a member variable of the type deriving from SoapHeader. Public TimeStamp As MyHeader
-
将 SoapHeader 特性应用于要处理 SOAP 头的每个 Web 服务方法。将 SoapHeader 特性的 MemberName 属性设置为第一步中创建的成员变量的名称。
C#[WebMethod] [SoapHeader("timeStamp")] public void MyWebMethod()
Visual Basic<WebMethod, SoapHeader("TimeStamp")> _ Public Sub MyWebMethod()
-
在应用 SoapHeader 特性的每个 Web 服务方法中,访问在第一步中创建的成员变量以处理在 SOAP 头中发送的数据。
C#[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!"); }
Visual Basic<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
示例
下面的代码示例演示如何在使用 ASP.NET 创建的 Web 服务中定义和处理 SOAP 头。MyWebService Web 服务具有一个名为 myHeaderMemberVariable 的成员变量,该成员变量属于从 SoapHeader (MyHeader) 派生的类型,设置为 SoapHeader 特性的 MemberName 属性。另外,SoapHeader 特性应用于指定 myHeaderMemberVariable 的 MyWebMethod Web 服务方法。在 MyWebMethod Web 服务方法中,通过访问 myHeaderMemberVariable 来获取 SOAP 头的 Username XML 元素的值。
<%@ 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="http://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:="http://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
在上一个示例中,如果对 MyWebMethod 的 SOAP 请求包含 UserName 元素设置为 Admin 的 MyHeader SOAP 头,还将执行附加的代码。即,以下 SOAP 请求将引起该代码的执行。
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Header> <MyHeader xmlns="http://www.contoso.com"> <Created>dateTime</Created> <Expires>long</Expires> </MyHeader> </soap:Header> <soap:Body> <MyWebMethod xmlns="http://www.contoso.com" /> </soap:Body> </soap:Envelope>