我们有一个接受请求的WCF服务,我们的合作伙伴正在向我们发送如下所示的XML:
<CreateCase xmlns="xyz">
<OtherNode></OtherNode>
</CreateCase>
为了让WCF正确分派,我们必须将XML包装在包装节点中,例如
<CreateCase xmlns="xyz">
<CreateCase>
<OtherNode></OtherNode>
</CreateCase>
</CreateCase>
我们在AfterReceiveRequest中执行以下操作:
logger.Log("Received Request - In AfterReceiveRequest Function");
System.ServiceModel.Channels.Message newMessage = null;
System.ServiceModel.Channels.MessageBuffer msgbuf = request.CreateBufferedCopy(int.MaxValue);
System.ServiceModel.Channels.Message tmpMessage = msgbuf.CreateMessage();
logger.Log("Created Message");
System.Xml.XmlDictionaryReader xdr = tmpMessage.GetReaderAtBodyContents();
logger.Log("Created XML Dictionary Reader");
XmlReader rdr = xdr.ReadSubtree();
logger.Log("Read Subtree");
XElement bodyEl = XElement.Load(rdr);
logger.Log("Read Body: " + bodyEl.ToString());
然后根据需要修改bodyEl并根据字符串重新创建body el /设置新消息:
//Recreate bodyEl from xml
bodyEl = XElement.Load(new MemoryStream(Encoding.UTF8.GetBytes(bodyXml)));
MemoryStream ms = new MemoryStream();
System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create(ms);
bodyEl.Save(xw);
xw.Flush();
xw.Close();
ms.Position = 0;
//create new message from modified XML document
System.Xml.XmlReader xr = System.Xml.XmlReader.Create(ms);
newMessage = System.ServiceModel.Channels.Message.CreateMessage(request.Version, null, xr);
newMessage.Headers.CopyHeadersFrom(request);
newMessage.Properties.CopyProperties(request.Properties);
request = newMessage;
当涉及到包含大量有效负载(25MB文件作为base64编码的字符串)的XML时,第一个XElement.Load似乎令人窒息,因为我在日志中看到的最后一项是“读取子树”。我不确定如何修改此代码以防止这种情况。
任何人都可以阐明这一点吗?