我有一個類MessageInspector的實現IDispatchMessageInspector,並在其BeforeSendReply方法我攔截所有WCF回覆,所以我可以壓縮數據之前發送郵件。數據是一個ByteArray。停止WCF MessageInspector從消息自動編碼Base64String
問題是,當我用壓縮的ByteArray重構消息時,WCF將消息編碼爲Base64String,然後將其發送出去,是否有任何方法來禁用此自動編碼?
代碼:
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;
}
}
回覆瀏覽器:
MTOM看起來很有趣,我會檢查出來。謝謝! – 2012-07-25 16:25:02