Search code examples
c#web-servicessoapasmx

Intercept SOAP messages from and to a web service at the client


I have a client that communicates with a web service. The class that I communicate with is a C# class that is generated through wsdl.exe. I now want to log all incoming and outgoing messages.

What I've done so far is to write a class that inherits from the automatically generated C# Class and I have overridden the GetReaderForMessage method. That way I can access the incoming message more or less like this:

protected override XmlReader GetReaderForMessage(SoapClientMessage message, int bufferSize)
{
    System.Xml.XmlReader aReader = base.GetReaderForMessage(message, bufferSize);
    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
    doc.Load(aReader);
    string content = doc.InnerXml.ToString();
    System.Xml.XmlReader aReader2 = System.Xml.XmlReader.Create(new System.IO.StringReader(content));

    return aReader2;
}

Obviously I'm not too happy with this solution, because basically I'm creating two xml readers. One to read the contents of the SOAP message and one to return to the method caller. Plus I can't really do the same with the GetWriterForMessage method.

But may be I'm just doing things too difficult to start with. Is it for instance possible to read the contents of the SoapClientMessage object directly? I've read some articles suggesting that I should use SoapExtensions here, but from what I can understand, that would only work if the 'client' that I am creating is itself a web service which in this case it is not.

Any suggestions?


Solution

  • I would suggest looking into using a SOAP extension, which in my opinion is ideal for this scenario. Here are a few links that describe the process.

    http://msdn.microsoft.com/en-us/magazine/cc164007.aspx

    https://ebay.custhelp.com/cgi-bin/ebay.cfg/php/enduser/std_adp.php?p_faqid=350

    http://www.codeproject.com/KB/webservices/efficientsoapextension.aspx