Is there an open specification for how to serialize to / deserialize from a BrokeredMessage?

83 Views Asked by At

This is directly related to this other question. When passing objects to and from a .NET Framework client, which uses BrokeredMessages, those BrokeredMessages aren't just using simple JSON or XML to serialize these objects. It seems to be a more proprietary format. So for example, to just pass the simple string value "test123" along, instead of this:

"   t   e   s   t   1   2   3   "
34  116 101 115 116 49  50  51  34

You get something more like this:

@   [ACKNOWLEDGE]   s   t   r   i   n   g   [BACKSPACE]   3
64  6               115 116 114 105 110 103 8             51

h   t   t   p   :   /   /   s   c   h   e   m   a   s   .   m   ...
104 116 116 112 58  47  47  115 99  104 101 109 97  115 46  109 ...

Ö   [BELL]   t   e   s   t   1   2   3
153 7        116 101 115 116 49  50  51

I've looked around, including both for and at the http://schemas... URL specified in the serialized example, but have not found a specification for how to serialize a BrokeredMessage. To some extent, I've been able to reverse engineer a way to send at least smaller strings between Messages in .NET Standard/Core and BrokeredMessages in .NET Framework, but this is heuristical and so on.

Is there a specification anywhere that spells out exactly what the serialization schema used by BrokeredMessages is?

1

There are 1 best solutions below

0
On

If the intent is to only grab the message body regardless of the content you can get it as a stream.

Stream stream = message.GetBody<Stream>();
StreamReader reader = new StreamReader(stream);
string s = reader.ReadToEnd();

As answered by ckross01 here.

To serialize complete BrokeredMessage to an object, below is the code -

public T GetBody<T>(BrokeredMessage brokeredMessage)
{
  var ct = brokeredMessage.ContentType;
  Type bodyType = Type.GetType(ct, true);

  var stream = brokeredMessage.GetBody<Stream>();
  DataContractSerializer serializer = new DataContractSerializer(bodyType);
  XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(stream, XmlDictionaryReaderQuotas.Max);
  object deserializedBody = serializer.ReadObject(reader);
  T msgBase = (T)deserializedBody;
  return msgBase;
}

As answered by yamspog here.