26 Nov 2015
768 is the Magic Number
When reconstrucing a binary MSMQ message from a backup I was first
tempted to just set the
Body property on the message directly. This
doesn’t quite work however as the
Body property is serialised with
whatever formatter you’re using when the message is sent.
var mq = new MessageQueue(@".\Private%\test"); var messageBody = File.ReadAllBytes("..."); var message = new Message(); message.Body = messageBody; mq.Send(message); // double-encodes the body :-(
There is a way around this serialisation though. MSDN has this to say:
The contents of the
Bodyproperty are serialized when the message is sent, using the
Formatterproperty you specify. The serialized contents are found in the
BodyStreamproperty. You can also set the
BodyStreamproperty directly, for example, to send a file as the data content of a message.
Sounds just like what we want! Just setting the
MemoryStream with our bytes in gets us started. The only
problem is that tools peeking at the message don’t know what format it
is in. This is where the magic number
768 makes it’s show. By
BodyType to this magic number tools will know the body
is serlialised with a
var message = new Message(); message.BodyStream = new MemoryStream(messageBody); message.BodyType = 768; // Binary formatted .NET object
Now you can send the message and recipients won’t know the differnece from the origional backup.