Using SOAP Extensions

Summary

Introduction

SOAP Extensions allow you to plug into the SOAP architecture of the .NET Framework in order to perform some pre- or post-processing on SOAP messages. When a SOAP message is received by the Web Service's HTTP handler, the SOAP message is de-serialized into objects (for example, SoapHeader for header information and SoapMessage for the body), and eventually passed into some Web Method. After the Web Method has finished, the result as well as any SoapHeader, SoapMessage, or SoapException objects are serialized into a SOAP message and sent back up the chain and to the client.

You can hook up into the request chain before and after the SOAP message is de-serialized into objects to be processed by the Web Service, and after the Web Service has finished processing the call and objects are serialized back into SOAP for transmission back to client.

When working with SOAP extensions, there are two stages for each call direction:

The following figure illustrates the overall process:

SOAP extensions differ from HTTP modules in a couple of things:

Purpose of SOAP Extensions

SOAP extensions are similar to custom sinks in .NET Remoting: Both are pluggable extensions into the .NET Framework that are used to perform some pre- or post-processing on some message. Like .NET Remoting, SOAP extensions can be used to encrypt/decrypt SOAP messages, to compress/decompress SOAP messages, and so on. In this chapter, SOAP messages are used to log the actual SOAP messages sent in requests and responses.

Creating SOAP Extensions

How to create a SOAP Extension

The basic steps to build a Web Service SOAP extension and have it run with an XML Web Service are:

How SOAP extensions are called

The following lists the order of invoking SOAP extension methods when invoking a Web Service method. The following assumes that the SOAP extension is running on both client and server.

Client Side
Server Side

An  Extension Example - Logger

This example uses SOAP extensions to capture the actual SOAP messages sent between a Web Service and its clients. It can be a great tool to debug and understand SOAP requests and responses. The following lists describes the classes used in this example:

The following illustrates a sample log file generated by the SOAP extension which allows you to look at the actual SOAP messages being sent around: 

**Begin SOAP Request - 3/24/2003 09:26:19
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <GetCustomer xmlns="http://tempuri.org/" />
    </soap:Body>
</soap:Envelope>
**End SOAP Request - 3/24/2003 09:26:19

**Begin SOAP Response - 3/24/2003 09:26:26
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <GetCustomerResponse xmlns="http://tempuri.org/">
            <GetCustomerResult>
                <strFirstName>Yazan</strFirstName>
                <strLastName>Diranieh</strLastName>
                <strCity>NetVille</strCity>
            </GetCustomerResult>
        </GetCustomerResponse>
    </soap:Body>
</soap:Envelope>
**End SOAP Response - 3/24/2003 09:26:26