【发布时间】:2017-05-11 08:23:33
【问题描述】:
我正在使用带有通用合同的 WCF。传入和传出消息的记录是通过 IDispatchMessageInspector.AfterReceiveRequest 和 IDispatchMessageInspector.BeforeSendReply 完成的
最近发现Message的内容和我输入的不一样。
我为回复生成的数据:
<OTA_PingRS xmlns="http://www.opentravel.org/OTA/2003/05">
<Success />
<EchoData>Just some text24</EchoData>
</OTA_PingRS>
使用普通阅读器发送。 elMessageResult就是上面的xml。
private Message CreateResponse(MessageVersion ver, XElement elMessageResult)
{
Message response = Message.CreateMessage(ver, "ProcessMessageResponse", elMessageResult.CreateReader());
return response;
}
BeforeSendReply 复制消息并读取 xml
public void BeforeSendReply(ref Message reply, object correlationState)
{
MessageBuffer buffer = reply.CreateBufferedCopy(Int32.MaxValue);
Message replyCopy = buffer.CreateMessage();
XDocument doc;
using (MemoryStream ms = new MemoryStream())
{
XmlWriter writer = XmlWriter.Create(ms);
replyCopy.WriteMessage(writer);
writer.Flush();
ms.Position = 0;
doc = XDocument.Load(ms);
}
if (SaveLog != null)
{
LogSaveFileEventArgs logEventArgs = new LogSaveFileEventArgs(doc, true);
SaveLog(this, logEventArgs);
}
reply = buffer.CreateMessage();
}
来自 BeforeSendReply 的 XDocument 文档包含更改的 xml。 Success 元素是 <Success></Success> 而不是 <Success />
<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<a:Action s:mustUnderstand="1">ProcessMessageResponse</a:Action>
<a:RelatesTo>urn:uuid:4D549F7F6D8D65FE891401181704779</a:RelatesTo>
</s:Header>
<s:Body>
<OTA_PingRS xmlns="http://www.opentravel.org/OTA/2003/05">
<Success></Success>
<EchoData>Just some text24</EchoData>
</OTA_PingRS>
</s:Body>
</s:Envelope>
奇怪的是,在客户端收到了带有正确 Success 元素的原始 xml。使用 SoapUI 进行了测试。
这可能是一个小问题,因为 xml 仍然有效,但谁知道还会改变什么。如果出现错误,我依靠适当的日志记录进行调查。
我已经玩过 XmlWriterSettings 没有成功。 有什么想法吗?
编辑:
replyCopy.ToString() 已包含格式错误的 xml。
【问题讨论】: