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: