Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Deserializing a Message

79 views
Skip to first unread message

N@discussions.microsoft.com Bill N

unread,
Mar 2, 2007, 6:33:15 PM3/2/07
to
I'm supporting some code written in VB.NET. I can't figure out how it works
as it seems to be able to cast the body as a string without deserializing as
shown below:
//////////////////////////////////////////////////////////////////////////////////////////
Private Sub GetAllMsgFromQueue(ByVal sQueueName As String, ByVal
objReceiveQueue As MSMQ.MSMQQueue)

On Error GoTo ErrorHandler

'MSMQ Message object to store messages from the queue
Dim objReceiveMessage As MSMQ.MSMQMessage
'Recordset which will hold one record per message, to be updated into the
database
Dim rsMessage As New ADODB.Recordset()
'Local Message variable
Dim sMessage As String

' Bind the recordset to the table we will be writing to
openRecordset(rsMessage, sQueueName)

‘Get the first message in the queue. This will not remove the message from
the queue.
objReceiveMessage = objReceiveQueue.PeekCurrent(, , 100)

'Loop through each message in the message queue
While Not objReceiveMessage Is Nothing
sMessage = objReceiveQueue.Receive.Body

'Call another procedure that parses the message
Call DoStuffMeThod(sMessage, rsMessage)

' go to the next message
objReceiveMessage = objReceiveQueue.PeekNext(, , 100)
End While

' Blah Blah and More stuff
//////////////////////////////////////////////////////////////////////////////////////////
When I try the same code in C# sharp as shown below, I get a message saying
“Cannot find a formatter capable of reading this message.” Anyone know how
the above code can work without throwing the same error?
//////////////////////////////////////////////////////////////////////////////////////////
private void btnPeekAtFirst_Click(object sender, EventArgs e)
{
System.Messaging.Message PeekErrMsg;
System.Messaging.Message PeekLogMsg;
string sMessage = "";

try
{
//now peek to see if label was returned
PeekErrMsg = _queError.Peek();
PeekLogMsg = _queLog.Peek();
sMessage = PeekErrMsg.Body.ToString();

// Blah Blah and More stuff
//////////////////////////////////////////////////////////////////////////////////////////

Frank Boyne

unread,
Mar 5, 2007, 10:55:35 PM3/5/07
to
"Bill N" <Bill N...@discussions.microsoft.com> wrote in message
news:4D89D0E6-4A6E-4365...@microsoft.com...
> Dim objReceiveMessage As MSMQ.MSMQMessage

Note that you are creating a queue instance using the COM object
MSMQ.MSMQMessage and not the System.Messaging interface. Presumably
your project has a COM Reference to the "Microsoft Message Queue 3.0
Object Library" (or some other version of the type library). Because
you are using the COM interface, the message body is interpreted as a
Variant with the body type property being used at the variant type.

I'm guessing the message you are processing ontains a string in te form
of a BSTR with VT_BSTR as the body type (or something else that looks
like a string).

> //////////////////////////////////////////////////////////////////////////////////////////
> When I try the same code in C# sharp as shown below, I get a message
> saying
> "Cannot find a formatter capable of reading this message." Anyone
> know how
> the above code can work without throwing the same error?

If you are getting that eror message then the code isn't the same, the
C# code is using the System.Messaging interface not theCOM interface.

You could change your C# program over to using teh COM interface by
removing the System.Messaging reference and adding a COM reference to
MSMQ in its place. Alternatively, you could use the
ActiveXMessageFormatter to get MSMQ to place the same interpretation on
body and body type that the COM interface uses.

_queError.Formatter = new ActiveXMessageFormatter (); // or
set formatter on message after Peek


> PeekErrMsg = _queError.Peek();
> PeekLogMsg = _queLog.Peek();

PeekErrMsg.Formatter = new ActiveXMessageFormatter (); // or
set formatter on Queue before Peek
> sMessage = PeekErrMsg.Body.ToString();


0 new messages