0

I have a class MessageInspector that implemenents IDispatchMessageInspector, and in its BeforeSendReply method I intercept all WCF replies so I can compress the data before the message gets sent. The data is a ByteArray.

The problem is that when I re-construct the message with the compressed ByteArray, WCF encodes the message as a Base64String and then sends it off, is there any way to disable this automatic encoding?

Code:

public void BeforeSendReply(ref Message reply, object correlationState)
{
    if (!reply.IsFault && !reply.IsEmpty)
    {
        //read json
        XmlDictionaryReader bodyReader = reply.GetReaderAtBodyContents();
        MemoryStream ms = new MemoryStream();
        XmlDictionaryWriter jsonWriter = JsonReaderWriterFactory.CreateJsonWriter(ms);
        jsonWriter.WriteNode(bodyReader, true);
        jsonWriter.Flush();

        //compress data
        byte[] ba = ms.ToArray();
        byte[] data = ZLibCompressor.Compress(ba);

        //rebuild and send reply
        Message newReply = Message.CreateMessage(MessageVersion.None, null, data);
        reply = newReply;
    }
}

Reply in browser:

WCF Get reply in Firefox

Community
  • 1
  • 1
Shahin Dohan
  • 6,149
  • 3
  • 41
  • 58

1 Answers1

1

Answers possibly here:

Getting around base64 encoding with WCF

http://social.msdn.microsoft.com/Forums/en/wcf/thread/c32a34c3-dfda-4c54-ab13-fd595fb883ab

And look at this one...you might be able to write the message as binary using CreateBinaryWriter.

Sending custom WCF Message to a service

http://www.techbubbles.com/webservices/wcf-service-using-mtom-in-net-fw-4/

Community
  • 1
  • 1
Colin Smith
  • 12,375
  • 4
  • 39
  • 47