Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Message Size

Tags:

wcf

Is there any way to test the size of a message before it is sent back to the client? I'd like to handle the maximum message size quota on the service side of things.

like image 454
mcass20 Avatar asked Feb 24 '11 17:02

mcass20


1 Answers

You can write a custom MessageEncoder, use it on the service side, and check an encoded message size inside it's WriteMessage methods.

Follow this guide and override the methods in such a way:

public override ArraySegment<byte> WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset)
{
    var result = this.text.WriteMessage(message, maxMessageSize, bufferManager, messageOffset);

    // Encoded message check logic.
    Console.WriteLine("Message sent. Length=" + result.Count);

    return result;
}

public override void WriteMessage(Message message, Stream stream)
{
    var position = stream.Position;

    this.text.WriteMessage(message, stream);

    // Encoded message check logic.
    Console.WriteLine("Message sent. Length=" + (stream.Position - position));
}
like image 55
stop-cran Avatar answered Oct 04 '22 15:10

stop-cran