desalasworks presents:

a selection of works by steven de salas

Logging SOAP Messages In .NET

C# Web Services provide an easy interface to incoming SOAP data because the SOAP message has already been deserialised at the entry point of a WebMethod.

The downside is that sometimes you’ll need to have access to the full SOAP request: body, headers and everything for tasks such as diagnosing any errors (which message broke the web service) or providing graceful handling of third party web services.

At first you’d think that the Request object can provide this information with methods such as Request.BinaryRead() but unfortunately when you’re using SOAP all you get there is a querystring.

The solution here is to use the SOAP Extensions. Here is some sample code:

public class SoapMessageLogger : SoapExtension
public override void ProcessMessage(SoapMessage message)
case SoapMessageStage.BeforeDeserialize:
LogResponse(message); break;
case SoapMessageStage.AfterSerialize:
LogResponse(message); break;

// Do nothing on other states
case SoapMessageStage.AfterDeserialize;
case SoapMessageStage.BeforeSerialize;
default: break;

More info on this can be found here and here.



  1. John Saunders says:

    I have a trivial issue with your article. There is no such thing as “C# Web Services”, any more than there are “VB.NET Web Services”. These are legacy ASMX web services (sometimes known as ASP.NET Web Services).

  2. Pregunton Cojonero says:

    http://www.codeproject.com/cs/webservices/SerializationFun.asp not found

    Using SoapExtension in Client ?

    C# Web SErvices vs VB.NET Web Services ??

Leave a Reply

(Spamcheck Enabled)