Monday, May 15, 2006

.NET Soap: Capturing Request and Response streams

There's just no simple way to capture the Raw Request and Response inside a SOAP server application in .NET. Of course you do have the Context.Request reference, which I thought was logically where the request would be. Boy, was I wrong.

It turns out that if you write code to access Context.Request.InputStream or use Context.Request.BinaryRead (which uses InputStream, as I decoded from Reflector) you will always get nothing. Zilch. Nada.

All sorts of google searches resulted in dead ends. In the end I found out about Soap Extensions on MSDN. This article shows you how you can add an attribute called TraceAttribute on your method like so:

 
[WebMethod]
[SoapInclude(typeof(ArrayList))]
[TraceExtension(Filename = @"C:\soaplog.txt")]
public ArrayList GetMyData()
....


and you will get a log of your request and response in the file mentioned.

I need to extend this to only log requests or responses, and to use TraceListeners if necessary. Also, this should be available as a stream rather than saving to a file only.

But I hope this helps people who need to see their request/response in a .NET SOAP WebService.