我在執行我的WCF消息攔截器時遇到了一些問題。基本上我正在訪問正文內容,並在節點集上執行xslt變換以對它進行排序。如何編輯WCF消息 - WCF消息攔截器
我測試過XSLT樣式表,它的工作沒有問題。我將轉換結果寫入MemoryStream對象,然後嘗試從流內容重新創建消息。
我考察使用一個StreamReader的所產生的數據流或加載到一個XmlDocument,我可以看到的XML它包含它從XSLT我預期的結果變換。當我嘗試重新消息
我的問題occures!我創建一個基於流的XmlReader並將其用作Message.CreateMessage(.....)的身體源代碼;
我不明白爲什麼我突然失去了「正確」的內容流中時,我可以檢驗一下,看看一對夫婦較早的語句。
非常感謝!下面的方法
全碼:
public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel, InstanceContext instanceContext)
{
MessageBuffer msgbuf = request.CreateBufferedCopy(int.MaxValue);
Message tmpMessage = msgbuf.CreateMessage();
XmlDictionaryReader xdr = tmpMessage.GetReaderAtBodyContents();
MemoryStream ms = new MemoryStream();
_compiledTransform.Transform(xdr,null,ms);
ms.Position = 0;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(ms);
MemoryStream newStream = new MemoryStream();
xmlDoc.Save(newStream);
newStream.Position = 0;
//To debug contents of the stream
StreamReader sr = new StreamReader(newStream);
var temp = sr.ReadToEnd();
//At this point the XSLT tranforms has resulted in the fragment we want so all good!
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;
newStream.Position = 0;
XmlReader reader = XmlReader.Create(newStream,settings);
reader.MoveToContent();
//Reader seems to have lost the correct fragment!!! At least returned message does not contain correct fragment.
Message newMessage = Message.CreateMessage(request.Version, null, reader);
newMessage.Properties.CopyProperties(request.Properties);
request = newMessage;
return request;
}
你可以發表一個原始和轉換的消息的例子嗎? – carlosfigueira 2011-05-27 03:20:37