Click here to Skip to main content
14,271,759 members

Trace SOAP Request/Response XML with TraceExtension

Rate this:
5.00 (14 votes)
Please Sign up or sign in to vote.
5.00 (14 votes)
17 Aug 2009CPOL
SOAP Request/Response XML can be logged with TraceExtension


In one of my recent projects, I had to interact with one .NET Web Service (ASMX) from a Windows Desktop application. Now another Java application would be accessing the same Web Service. So the Java developers need a sample SOAP Request/Response XML for the Web Service. Before I start discussing the approach of logging SOAP Request-Response XML, I would like to elaborate on same basic things Web Service referencing.

What’s WSDL File and How It’s Generated?

Web Services Description Language is an XML format for describing network services as a set of endpoints operating on messages containing either document-oriented or procedure-oriented information. In a single sentence – it defines the Web Service which can be used by the clients to subscribe to the web service. In case you are not aware of generating a WSDL file of a Web Service, then follow the steps mentioned below:

  1. Type in your Web Service URL along with the ASMX file in the web browser.
  2. Then just append “?wsdl” at the end of URL, your required WSDL will be generated in the browser. So if your URL is http://localhost/HelloWorldWebService/HelloWorldService.asmx then if you type in http://localhost/HelloWorldWebService/HelloWorldService.asmx?wsdl to generate the wsdl for your web service.

How to Reference a Web Service?

This is quite easy for Visual Studio users. Using the wizard of Add Web Reference (as shown below in the images), the developer just needs to provide the URL of the Web Service. This would generate the proxy and the developer does not even need to create the WSDL separately.

Add WebReference

WebReference URL

Another approach could be generation of proxy class from WSDL file by using the WSDL.exe. Once proxy is generated, proxy class could be used for interaction with the Web Service.


Simple Object Access Protocol is a simple XML-based protocol to let applications exchange information over HTTP. SOAP is a protocol for accessing a Web Service.

A SOAP message is an ordinary XML document containing the following elements:

  • An Envelope element that identifies the XML document as a SOAP message
  • A Header element that contains header information
  • A Body element that contains call and response information
  • A Fault element containing errors and status information

The following image displays a sample SOAP Request and Response XML.

Sample SOAP Request Response XML

What’s SOAP Extension – TraceExtension?

SOAP Extensions allow developers to create very interesting applications on top of the core SOAP architecture found within .NET. It allows developers to implement encryption algorithm, compression routine, SOAP attachments, etc. In this article, we will focus on logging the SOAP request/response XML.

For this, we need to create a couple of classes:

  • Create a class that derives from System.Web.Services.Protocols.SoapExtension TraceExtension class has been created for this.
  • Create a class that derives from System.Web.Services.Protocols.SoapExtensionAttribute TraceExtensionAttribute class has been created for this.

A Sample Web Service Application

To understand the functionality of TraceExtension class, I will take a sample WebService and a client which subscribes to that service.

  • HelloWorldWebService – This is a web service with a web method HelloWorld.
    public class HelloWorldService : System.Web.Services.WebService
            public string HelloWorld(string myString)
                 return "Hello World Service returns - " + myString;
  • ClientSubscriber – This is a simple Windows Form application consuming the web reference of HelloWorldWebService. In the form, there’s a textbox and button. On click of this button, the text of the textbox will send to the web service and the result will displayed in a message box.
  • SoapMessageUtility – This utility has the actual implementation of TraceExtension. The client application has a reference to this utility to log the SOAP request/response XML.

Implementation of TraceExtension

We will implement the TraceExtension in the client side consuming the web service. So the Request/Response SOAP XML logs will be created in the client side.

First we will take look at the TraceExtensionAttribute class inherited from SoapExtensionAttribute. TraceExtensionAttribute must override the ExtensionType property to return the type of extension that is associated with the attribute. In this case, this is nothing but TraceExtension.

public override Type ExtensionType     
    get { return typeof(TraceExtension); }

For setting the priority of the ‘SoapExtension’, Priority property has been overridden too.

To have some flexibilities, few properties are created in the TraceExtensionAttribute which can be passed as parameters in the constructor.

  1. LogTypeMode – This denotes which SOAP XML to log – None, RequestOnly, ResponseOnly, RequestReponse
  2. ReqFileName – The file name where Request SOAP XML will be logged
  3. ResFileName – The file name where Response SOAP XML will be logged

TraceExtensionAttribute class can be used at method level as shown below:


	("", RequestNamespace=, 
public string HelloWorld(string myString) {
    object[] results = this.Invoke("HelloWorld", new object[] 
	{                        myString});
    return ((string)(results[0]));

Now we will look into the TraceExtension class inherited from SoapExtension. The core piece of implementation is the ProcessMessage method of the SoapExtension class. This method should be overridden to have the proper implementation. Before this implementation, we should be very much aware of the flow of SOAP message from the client to Web Service.

  1. When a web method in the client proxy is called, the framework checks if any SOAP extensions are to be invoked or not and if so, then those are called with BeforeSerialize stage.
  2. Once the serialization is done, then the extensions are called with AfterSerialize stage.
  3. The SOAP message is sent to the server and server figures out which method to route to.
  4. Then the server checks to see if any SOAP extensions (that’s us!) should be invoked, and if so, invokes them with the BeforeDeserialize event stage.
  5. The server deserializes the stream and invokes all the extensions for the AfterDeserialize stage.
  6. After the web method execution server invokes all the extensions with BeforeSerialize stage.
  7. Once the server serializes the result stream, the SOAP extensions are called with AfterSerialize stage.
  8. Result is sent back to client
  9. Now client receives the result stream and framework invokes the SOAP extensions with BeforeDeserialize event stage.
  10. Once client deserializes the result stream, SOAP extensions with AfterDeserialize stage is invoked.

In our case, we will be focused in the client side, i.e. points 1,2,9 and 10.

The key is the AfterSerialize stage (i.e. point 2) in the client, the request SOAP XML can be logged at this moment.

public void WriteOutput(SoapMessage message)
    FileStream fs;
    StreamWriter w = null;
        if (_logTypeMode.Equals(LogType.RequestOnly) || 
            _newStream.Position = 0;
            fs = new FileStream(_reqFilename, FileMode.Append,
            w = new StreamWriter(fs); 
            string soapString = "SoapRequest";
            w.WriteLine("-----" + soapString + " at " + DateTime.Now);
            Copy(_newStream, fs);
    catch (Exception ex)
        if (w != null)
        _newStream.Position = 0;
        Copy(_newStream, _oldStream);

The same is applicable for the BeforeDeserialize stage (i.e. point 9) to log the response SOAP XML. The WriteInput method logs the response XML.

Safety and Configurability

The whole purpose of this article is to log the request-response SOAP XML. In case of any exception while logging, it would cause interruption in the web service invocation. So the WriteInput and WriteOutput methods in TraceExtension class should take care of proper exception handling. In my case, I kept a blank catch block which can be modified with some exception logging mechanism but that should not impact the normal web service invocation.

Already I tried to provide some flexibilities in the TraceExtensionAttribute with few properties. These can be set while instantiating the attribute in the method level. Also, TraceExtension class can support the following entries in (config files web.config/app.config).

  <add key ="REQ_LOGFILE" value="c:\log\SOAPReq_log.txt"/>
  <add key ="RES_LOGFILE" value="c:\log\SOAPRes_log.txt"/>

  <add key ="LogTypeMode" value="3"/>
<!--<span class="code-comment"> None = 0,
      RequestOnly = 1,
      ResponseOnly = 2,
      RequestReponse = 3 --></span>

If both config entries and attribute parameters are provided, then config entries would override that. So my utility (i.e. SoapMessageUtility.dll) can be directly used with the above configuration entries.


I would suggest you all to have a look into the MSDN link on this. Also for beginners to SOAP, the tutorial from w3schools might be helpful.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Arindam Sinha
Software Developer (Senior)
India India
I have been working in IT industry for the last 6 years.
My comfort zone is .NET and Microsoft technologies.

Comments and Discussions

QuestionGetting Request/Response back to ClientSubscriber Pin
Member 949085329-Dec-15 15:20
memberMember 949085329-Dec-15 15:20 
AnswerRe: Getting Request/Response back to ClientSubscriber Pin
Member 949085323-Jan-16 13:00
memberMember 949085323-Jan-16 13:00 
QuestionError in article! Pin
affv16-Apr-15 3:26
memberaffv16-Apr-15 3:26 
QuestionCapturing HTTP Request/Response Pin
Member 862129525-Nov-13 3:06
memberMember 862129525-Nov-13 3:06 
QuestionServer side solution Pin
blavod862-Apr-13 21:56
memberblavod862-Apr-13 21:56 
GeneralMy vote of 5 Pin
Rai Pawan28-Dec-12 0:24
memberRai Pawan28-Dec-12 0:24 
Questionneed step-by-step guide for implement this trace Pin
Tridip Bhattacharjee19-Nov-12 3:14
professionalTridip Bhattacharjee19-Nov-12 3:14 
QuestionLogging xml part is not working Pin
Tridip Bhattacharjee7-Nov-12 1:18
professionalTridip Bhattacharjee7-Nov-12 1:18 
QuestionDownloaded-started_does not work Pin
Member 30817928-Oct-12 7:00
memberMember 30817928-Oct-12 7:00 
Questionserver side Pin
savol9-Apr-12 23:59
membersavol9-Apr-12 23:59 
TheCubanP9-Feb-12 9:42
memberTheCubanP9-Feb-12 9:42 
GeneralRe: DOESN'T WORK Pin
Member 944119419-Sep-12 3:17
memberMember 944119419-Sep-12 3:17 
GeneralMy vote of 5 Pin
divetramp10-Oct-11 5:38
memberdivetramp10-Oct-11 5:38 version Pin
acsjimmy20-Jul-11 22:29
memberacsjimmy20-Jul-11 22:29 
GeneralGeneral question - I am not gettting a Well response Pin
Amer Azzaz29-Mar-10 3:03
memberAmer Azzaz29-Mar-10 3:03 
QuestionCan this be extended to log Attributes Pin
manu_cm9-Dec-09 3:19
professionalmanu_cm9-Dec-09 3:19 
Questionincorrect line? Pin
demobb16-Sep-09 9:43
memberdemobb16-Sep-09 9:43 
AnswerRe: incorrect line? Pin
Arindam Sinha31-Mar-10 22:49
memberArindam Sinha31-Mar-10 22:49 
GeneralMissing Image Pin
AspDotNetDev18-Aug-09 20:56
protectorAspDotNetDev18-Aug-09 20:56 
GeneralRe: Missing Image Pin
Arindam Sinha22-Aug-09 5:51
memberArindam Sinha22-Aug-09 5:51 
GeneralIndented Code Snippets Pin
AspDotNetDev18-Aug-09 20:54
protectorAspDotNetDev18-Aug-09 20:54 
GeneralRe: Indented Code Snippets Pin
Arindam Sinha22-Aug-09 5:54
memberArindam Sinha22-Aug-09 5:54 
GeneralCatching Exceptions Pin
AspDotNetDev18-Aug-09 20:50
protectorAspDotNetDev18-Aug-09 20:50 
GeneralRe: Catching Exceptions Pin
Arindam Sinha22-Aug-09 6:04
memberArindam Sinha22-Aug-09 6:04 
GeneralRe: Catching Exceptions Pin
AspDotNetDev22-Aug-09 10:07
protectorAspDotNetDev22-Aug-09 10:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Technical Blog
Posted 17 Aug 2009


33 bookmarked