If ever you're needing to debug the interaction between a Web Service and its clients, Microsoft’s Fiddler is the tool to use - this includes WCF Services so long as they're using a HTTP transport. The only thing Fiddler won't do is decode messages that are sent using WCF's proprietary Binary encoding - until today, that is: at lunch time, I took advantage of Fiddler's neat extensibility mechanism and created a rough-and-ready Inspector that will translate binary messages from gobbledegook to plain xml for your debugging pleasure.
You can download the plug-in and source from MSDN Code Gallery. To use it, just drop the plug-in in the Inspectors folder of your Fiddler installation. Once you've reloaded Fiddler, switch to the Inspectors tab and look for WCF Binary.
Implementation Notes
- There’s a very helpful page on the Fiddler site which tells you how to build Inspectors in .Net.
- Fiddler gives each Inspector the raw bytes of each message, and it can do with it what it likes. Here’s how I decode a WCF Binary encoded message:
using System; using System.Runtime.Serialization; using System.ServiceModel.Channels; ... private static readonly BufferManager _bufferManager = BufferManager.CreateBufferManager(int.MaxValue, int.MaxValue); ... private string GetWcfBinaryMessageAsText(byte[] encodedMessage) { var bindingElement = new BinaryMessageEncodingBindingElement(); var factory = bindingElement.CreateMessageEncoderFactory(); var message = factory.Encoder.ReadMessage(new ArraySegment<byte>(encodedMessage), _bufferManager); return message.ToString(); }
19 comments:
Awesome Sam!
Sam, This is great. Appreciate it!!!
doesn't work for me i get this error
System.Xml.XmlException: The input source is not correctly formatted.
at System.Xml.XmlExceptionHelper.ThrowXmlException(XmlDictionaryReader reader, String res, String arg1, String arg2, String arg3)
at System.Xml.XmlBufferReader.ReadValue(XmlBinaryNodeType nodeType, ValueHandle value)
at System.Xml.XmlBinaryReader.ReadNode()
at System.Xml.XmlBinaryReader.Read()
at System.Xml.XmlBaseReader.IsStartElement()
at System.Xml.XmlBaseReader.IsStartElement(XmlDictionaryString localName, XmlDictionaryString namespaceUri)
at System.ServiceModel.Channels.ReceivedMessage.ReadStartEnvelope(XmlDictionaryReader reader)
at System.ServiceModel.Channels.BufferedMessage..ctor(IBufferedMessageData messageData, RecycledMessageState recycledMessageState, Boolean[] understoodHeaders)
at System.ServiceModel.Channels.BinaryMessageEncoderFactory.BinaryMessageEncoder.ReadMessage(ArraySegment`1 buffer, BufferManager bufferManager, String contentType)
at System.ServiceModel.Channels.MessageEncoder.ReadMessage(ArraySegment`1 buffer, BufferManager bufferManager)
at BinaryMessageFiddlerExtension.BinaryInspector.GetWcfBinaryMessageAsText(Byte[] encodedMessage)
at BinaryMessageFiddlerExtension.BinaryInspector.UpdateView(Byte[] bytes)
Shaihan,
Have you checked that the message is actually encoded in WCF Binary form? This is the kind of stack trace you get when looking at a message encoded in some other form.
i am not sure. aren't the messages supposed to be sent in binary format in wcf by default? or do i have to explicitly instruct that? i am new into wcf. also i am new to fiddler as well. is there any way to check the message in xml format?
forgot to select the profile. the above comment is mine. thanks for the help
Messages are not always sent in Binary - it depends what binding you are using. If you are using HttpBinding, then the default is SOAP encoding using XML. To see whether the message is in XML format, just switch to the XML tab in the inspectors window in Fiddler
I always had problems with binary coding, it is so complicated for me. Thanks for some advices about that you included in your post.
This doesnt work for me I get the following error:
System.Xml.XmlException: The prefix 'q' is not defined.
at System.Xml.XmlExceptionHelper.ThrowXmlException(XmlDictionaryReader reader, String res, String arg1, String arg2, String arg3)
at System.Xml.XmlBaseReader.LookupNamespace(PrefixHandleType prefix)
at System.Xml.XmlBinaryReader.ReadNode()
at System.Xml.XmlBinaryReader.Read()
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at BinaryMessageFiddlerExtension.BinaryInspector.LoadMessageIntoDocument(Byte[] encodedMessage)
at BinaryMessageFiddlerExtension.BinaryInspector.GetWcfBinaryMessageAsText(Byte[] encodedMessage)
at BinaryMessageFiddlerExtension.BinaryInspector.UpdateView(Byte[] bytes)
Thanks Sam.
Much appreciated, it's a great plugin.
I discovered your web site via Google while looking for a related subject, lucky for me your web site came up, its a great website. I have bookmarked it in my google bookmarks.
Please Visit ===> Physician Assistant Training Class
Will this work for fiddler version 2.3.2.4? its not working for me...
@Aravind, I haven't tried it, to be honest. Remember that the source is available if you want to find out why it's not working :-)
Very nice!
This is amazing - thanks so much!
Just one thing - in the first sentence of your article, you mention that Fiddler is a Microsoft tool. This is incorrect - it is written and distributed by Eric Lawrence.
"Microsoft’s Fiddler"...lol. Nice plugin
It was created by someone from MS.
Where can I download this? It is no longer in the MS code gallery. This link is dead: http://code.msdn.microsoft.com/wcfbinaryinspector
Hi Tim. I've not done anything with this for a while. But it looks like https://github.com/waf/WCF-Binary-Message-Inspector is a good replacement.
Post a Comment