Pages

Monday, 2 November 2009

A Fiddler plug-in for inspecting WCF Binary encoded messages

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.WCFBinaryFiddlerPlugin

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:

  1. Sam, This is great. Appreciate it!!!

    ReplyDelete
  2. 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)

    ReplyDelete
  3. 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.

    ReplyDelete
  4. 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?

    ReplyDelete
  5. forgot to select the profile. the above comment is mine. thanks for the help

    ReplyDelete
  6. 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

    ReplyDelete
  7. I always had problems with binary coding, it is so complicated for me. Thanks for some advices about that you included in your post.

    ReplyDelete
  8. 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)

    ReplyDelete
  9. Thanks Sam.
    Much appreciated, it's a great plugin.

    ReplyDelete
  10. 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
     

    ReplyDelete
  11. Will this work for fiddler version 2.3.2.4? its not working for me...

    ReplyDelete
  12. @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 :-)

    ReplyDelete
  13. 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.

    ReplyDelete
  14. "Microsoft’s Fiddler"...lol. Nice plugin

    ReplyDelete
  15. It was created by someone from MS.

    ReplyDelete
  16. Where can I download this? It is no longer in the MS code gallery. This link is dead: http://code.msdn.microsoft.com/wcfbinaryinspector

    ReplyDelete
  17. 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.

    ReplyDelete