Decode bodysection back to json

Jul 13, 2015 at 6:19 PM

I cant seem to find an example of how to decode the binary data held within the bodysection back to json.

for example:
private void OnMessageCallback(ReceiverLink receiver, Message message)
                var bAccept = true;
                switch (message.Properties.Subject)
                    case "NewProgramAvailable":
                            //TODO: download firmware using the json contained in the body of the message
                            //var info = JsonSerializer.DeserializeString(Encoding.UTF8.GetChars(message.BodySection).ToString()) as Hashtable;

                            //if (info.Contains("url")) //&& info.Contains("fileName"))
                            //    WebClient.DownloadFile(info["url"].ToString(), sdCard.StorageDevice.RootDirectory + @"Application.hex");
An example would be much appreciated.


Jul 14, 2015 at 12:42 AM
BodySection is an AMQP type that contains the user's payload. To get the unwrapped body, you can reference Message.Body property.

Depending on how the message was sent, the Body property is likely a string object or a byte[] object. The byte[] should contains the bytes of the JSON string encoded by the text encoder you chose.
Aug 11, 2015 at 10:04 PM
Thanks for your reply and sorry for my delayed response (moving house!)

I have made some progress on this today and it seems that it maybe because the originating message comes from Microsoft.ServiceBus.Messaging.BrokeredMessage which causes the body section to contain some bytes mostly containing ASCII before the actual set body e.g.
It is the bytes that are not ASCII that makes the body hard to decode...

as a test I have:
var data = message.BodySection as Data;
                            //TODO: this should not need to be done i.e. removing the fixed header from the body of the message. it could be a result of using servicebus to generate the message and this should be checked!
                            byte[] test = data.Binary;
                            var len = test.Length - 68;
                            byte[] test2 = new byte[len];
                            var j = 0;
                            for (var i = 69; i < test.Length; i++)
                                test2[j] = test[i];

                            var s = new string(Encoding.UTF8.GetChars(test2));

I am not sure if this will be solved by using your library to create the originating message, but it would be nice if there was a way of handling messages from servicebus in a more dignified manor...


Aug 11, 2015 at 10:28 PM
The message was sent by the default network protocol (NetMessaging) of the SB SDK. If you created the BrokeredMessage with the body being the JSON string, the body is serialized using a binary DataContractSerializer, which is an XML serializer. That's why you see the non-ascii bytes and an XML namespace at the beginning. When the message is received by the AMQP.Net Lite client, the SB service converts the message into a standard AMQP message format but the body part (serialized bytes) is sent as is because it is completely opaque. In this case, the application is responsible for decoding the body.

There are a few options you could do to make it work:
  • Create a DataContractSerializer from type string and read the JSON object from the bytes using a binary reader.
  • Use the AMQP protocol in the SB client to send the message (by setting the messaging factory's transport type to Amqp).
  • Use AMQP.Net Lite on both send and receive side.

Aug 11, 2015 at 10:36 PM
Thanks, that makes sense. I will give it a try.