System.Messaging.Message Plain Text like MSMQ

I'm converting a whole bunch of code from VB6 that uses MSMQ.  Instead of using the MSMQ Interop COM libs I'm converting to System.Messaging.  There are some message differences.

The big difference is the the message body by default is encoded as XML.  That's great for the newer projects I'm working on, but the old projects depend on plain text.  So how do you create a System.Messaging.Message for plain text.  I googled and searched but no simple code snippet in VB.  So here is my code

UPDATE:  The messages were still not correct until I added the BodyType =8.  Using the ActiveXFormatter creates a BodyType of 31 VT_LPWSTR which will not work with the COM+ MSMQ

            Dim messageQueue As String = "myMachine\myTestQueue"
            Dim messageLabel As String = "Test"
            Dim messageBody As String = "Test Text Message"

            Dim oQueue As New Messaging.MessageQueue(messageQueue)
            Dim oMessage As New Message

            oMessage.Label = messageLabel
            Dim writer As New IO.StreamWriter(oMessage.BodyStream, System.Text.Encoding.ASCII)

           oMessage.BodyType = 8 'Need to set for string VT_BSTR

            oQueue.Send(oMessage, MessageQueueTransactionType.Single)

 For unicode encoding use this writer
            Dim writer As New IO.StreamWriter(oMessage.BodyStream, New System.Text.UnicodeEncoding(False, False))
 The text will be written as unicode without the byte marker

Print | posted on Monday, February 1, 2010 5:41 PM