Buffer size for Message in IClientMessageInspector.BeforeSendRequest

I wanted to inspect messages in the BeforeSendRequest method of IClientMessageInspector. The first line of code that I write is:

MessageBuffer buffer = request.CreateBufferedCopy(13000);

The number represents maximum buffer size. This forces me to know the message size before hand. If I am writing a generic inpsector, I may not know the size yet I am forced to make a decision here. Are there any other patterns to inspect the messages other than creating the message buffer? Here is my simple implementation:

public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
Console.WriteLine("BeforeSendRequest()");

//Create the buffer
MessageBuffer buffer = request.CreateBufferedCopy(13000);

//Create another message for inspection as we can not use request argument any more.
//It seems add that we did not do any thing with request argument except to create
//the message buffer. Since creating buffer requires the message to be read, this argument
//is not good any more.
Message thisRequest = buffer.CreateMessage();

//Inspect the request (for now simply extract the body contents)
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
//settings.IndentChars = @"\t";
XmlDictionaryReader reader = thisRequest.GetReaderAtBodyContents();
StringBuilder info = new StringBuilder();
XmlWriter writer = XmlWriter.Create(info, settings);
writer.WriteNode(reader, true);
writer.Flush();

//So create another copy and assign it to the reply
request = buffer.CreateMessage();

//Close the buffer
buffer.Close();

Utility.DumpConsoleMessage(info.ToString(), ConsoleColor.Yellow);

return null; //Correlation object to be used in AfterReceiveReply method
}

Any help is appreciated.

Thanks.

[1881 byte] By [RRaghu] at [2007-12-22]
# 1

can you try this approach

MessageBuffer msg = request.CreateBufferedCopy(request.ToString().Length);

request = msg.CreateMessage();

MadhuPonduru-MSFT at 2007-8-30 > top of Msdn Tech,Visual Studio Orcas,Windows Communication Foundation (Indigo)...
# 2

It worked. Thank you.

RRaghu at 2007-8-30 > top of Msdn Tech,Visual Studio Orcas,Windows Communication Foundation (Indigo)...

Visual Studio Orcas

Site Classified