Exploring the Header in the WSE 2.0 SoapEnvelope Class

This article, the second of two parts, continues our exploration of the SoapEnvelope class in WSE2.0 by examining some of its properties and methods. In particular, we focus on manipulating the SoapHeader.

Contributed by
Rating: 3 stars3 stars3 stars3 stars3 stars / 2
January 15, 2007
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Soap

SOAP is well known as an extremely lightweight protocol requiring fewer resources from the sender or recipient of a communication. Since it is XML-based, any system capable of parsing XML should be capable of communicating using this protocol. The basic unit of communication in SOAP is a SOAP Message. WSE2.0 provides a large number of classes in the Microsoft.Web.Services2 name space that make the communication possible with flexible lightweight web services. A partial list of related classes in this name space include the following:

Soap 
Soap.AttributeName
Soap.ElementNames
SoapContext
SoapEnvelope
SoapFault
SoapFormatException
SoapInputFilter
SoapInputFilterCollection
SoapOutputFilter
SoapOutputFilterCollection
SoapWebRequest 
SoapWebResponse 

SOAP Message

A SOAP message consists of a Header, a Body and any other independent elements housed in what is called a SOAP Envelope.

SOAP Headers

The function of the envisaged use of a SOAP Header is to provide support for extending the message content. The SOAP header might be used for message routing, transaction management, user authentication, etc.

The XML grammar rules applies to the SOAP Header as to its name (Header) and its position in the SOAP Envelope (the first immediate child of Soap Envelope); any other entries in the Header are its children and they are all namespace qualified. Other entries in the Header may be used by the intermediaries in the message route for processing of the message by that node.

As discussed earlier, in the first part of this two part series, while the Body element is mandatory in the SOAP Message, the Header is optional. There is a difference in how the SOAP specification treats the Header element in a SOAP message, and how Microsoft defines the Soap Header. Visual Studio IDE 2003 can manipulate headers.

The SOAP specification does not strictly define the headers. Therefore, a class containing the data in the header has to be created for adding header information for web services created by the IDE. This class is derived from the SoapHeader class. This tutorial shows how this is done.

Manipulating SoapHeader in Visual Studio .NET 2003

Create a new project of type web service, called Hdrs in this tutorial. Follow the earlier steps discussed in part one of this series to add a reference to WSE2.0 and a new web service, named SopHdr.asmx in this tutorial. Since we would like to look at the SoapEnvelope's Header property we will create a Public Class jhdr whose code is shown here [HdrInfo.vb] after the picture.

Code for the Public Class

As noted previously, here is the code:

Imports System Imports System.Web.Services.Protocols Namespace
SoapHeader Public Class jhdr Inherits
System.Web.Services.Protocols.SoapHeader Public UserName As
String End Class End Namespace

The code for the SopHdr.asmx is shown here:

Imports System.Web.Services.Protocols Imports Microsoft.Web.Services2 Namespace SoapHeader <System.Web.Services.WebService(Namespace:="http://tempuri.org/Hdrs/
SopHdr")> _ Public Class SopHdr Inherits System.Web.Services.Protocols.SoapHeader Public myHeader As New jhdr #Region " Web Services Designer Generated Code " <WebMethod(), SoapHeader("myHeader")> _ Public Function TestHdr() As String Dim sen1 As New Microsoft.Web.Services2.SoapEnvelope myHeader.UserName = "mysorian" sen1.SetBodyObject("OK") sen1.InnerXml = "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> <soap:Header/> <soap:Body> </soap:Body> </soap:Envelope>" sen1.Header.InnerXml = myHeader.UserName Try 'Return sen1.Header.IsEmpty Return sen1.Header.OuterXml Catch ex As SoapHeaderException Return ex.Message End Try End Function End Class End Namespace

The following items are to be noted:

WSE2.0 is properly referenced by the imports statement. The statement regarding the inheritance from the System.Web.Services.protocols is included. An instance of the class jhdr is included. Also the SoapHeader reference to the jhdr class is referenced in the Web Method.

With the above in place, a Public function is scripted to get the SoapEnvelope's Header information using the above code. First of all a reference to the SoapEnvelope from WSE2.0 is instantiated with sen1. The SoapEnvelope should have a Body element; hence, a dummy body ["OK"] is set using the SetBodyObject method. Also the root element must be present; this is supplied by the SoapEnvelope's InnerXML setting.

Look at the complete string of the SoapEnvelope. You will observe that an empty SoapHeader element is added. With this preliminary preparation, the method can take all this information and return the SoapEnvelope's header from the web service. The commented return should produce a false since the header is not empty. Errors in the SoapHeader can be thrown as SoapErrorException. For example, if you were to drop the <Soap:Header/> element in the SoapEnvelope string an exception would be thrown.

Running the Code

As it is, the code produces the following output when run.

Clicking on TestHdr link will bring up the next web page:

Review the details of SOAP above. The Soap:Header element has the class information. TestHdrResponse results in a string (formatting the OuterXML of the SoapEnvelope's header). By clicking the Invoke button, the method call is made and the result is seen in the browser as shown here:

 

Summary:

The article has explored the properties and methods of the SoapEnvelope Class in WSE2.0's Microsoft.Web.Services2 namespace. The codes can be used for reference, and they can be copied and pasted while testing. Since SoapHeader is treated differently in the Visual Studio IDE, a class was designed to incorporate this information.

The SoapHeader code was tested using the SopHdr.asmx file, but the information in the header can be passed to the web service via client applications. In fact, web service authentication can be carried out incorporating the credentials in the SoapHeader [Reference: Professional ASP.NET Web Services, Russ Bassiura et al, Wrox Press, ISBN: 1-861005-45-8, 2001].

blog comments powered by Disqus
WINDOWS SCRIPTING ARTICLES

- More Windows Scripting Workarounds from Nilpo
- Overloading Methods and More in VBScript
- Improving MFC for Windows Vista
- Regular Expressions in VBScript
- Working with Dates in WMI
- Completing Calendars with VBScript Date Func...
- Building Calendars with VBScript Date Functi...
- Working With Dates and Times in VBScript
- Designing WCF DataContract Classes Using the...
- Understanding Dates and Times in VBScript
- Working With Arrays in VBScript
- Compressed Folders in WSH
- Using .NET Interops in VBScript
- Nilpo`s Scripting Secrets, Vol I
- Database operations using Silverlight 2.0 WC...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials