如何在wcf检查器中获取消息体

时间:2012-09-17 16:42:16

标签: wcf text inspector

我需要监控服务的响应,我得到方法GetReaderAtBodyContents()的正文,但是当WCF发送错误消息时,正文无法读取,因为消息没有xmlelement("Text cannot be written outside the root element.")?我看到该文字不能有<binary>。谁知道有错误消息?

2 个答案:

答案 0 :(得分:1)

消息格式为RAW,我读取正文:

var bodyReader = message.GetReaderAtBodyContents();
            bodyReader.ReadStartElement("Binary");
            var bodyBytes = bodyReader.ReadContentAsBase64();
            writer = XmlDictionaryWriter.CreateBinaryWriter(ms);
            writer.WriteStartElement("Binary");
            writer.WriteBase64(bodyBytes, 0, bodyBytes.Length);
            writer.WriteEndElement();
            writer.Flush();
            ms.Position = 0;
            reader = XmlDictionaryReader.CreateBinaryReader(ms, XmlDictionaryReaderQuotas.Max);
            body = Encoding.UTF8.GetString(bodyBytes);
            ms.Position = 0;
            newMessage = Message.CreateMessage(reader, int.MaxValue, message.Version);
            CopyMessagePropertiesAndHeaders(message, newMessage);
            message = newMessage;

但是如果返回错误消息,GetReaderAtBodyContents()引发错误,因为消息没有xmlelement <Binary>,如何解决它?

答案 1 :(得分:0)