I have been asked this question many times and thought I just put a quick note here.
If you want to access the message from a WCF service operations then you need to distinguish two cases.
Strongly-typed contracts
public string Hello(string hello)
{
Console.WriteLine(OperationContext.Current.RequestContext.RequestMessage.ToString());
return hello;
}
Universal contracts
public void Process(Message message)
{
Console.WriteLine(message.ToString());
}
But calling ToString() on the message itself will only print <stream>…</stream> if it is a streamed message.
In these cases you may need to do something similar like the following snippet in order to print the body of the streamed message.
XmlTextWriter xtw= new XmlTextWriter(Console.Out);
xtw.Formatting = Formatting.Indented;
message.WriteMessage(xtw);
writer.Flush();
writer.Close();
RequestContext.RequestMessage is not going to be present on one-way calls, so it's a partial solution at best. I've encountered this problem myself in the past, and ended up writing a message inspector for caching the messages: http://blogs.microsoft.co.il/blogs/sasha/archive/2008/06/15/obtaining-an-untyped-wcf-message-from-a-typed-service-operation.aspx
Posted by: Sasha Goldshtein | 06/29/2008 at 12:44 AM