tags:

views:

416

answers:

1

How would you avoid the InvalidOperationException created by the MSMQ sender and receiver below? Any help you can offer would be greatly appreciated!

I have a feeling it might be related to the BodyType attribute of the sender, but I do not know the proper domain of values for the attribute.

Sender code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Messaging;

namespace MSMQTester
{
    class Program
    {
        static void Main(string[] args)
        {
            MessageQueue q = new MessageQueue(@"lab\test");
            q.Formatter = new ActiveXMessageFormatter();
            Message msg = new Message();
            msg.Priority = MessagePriority.High;
            msg.Body = "<hello_world />";
            msg.Label = "test1";
            q.Send(msg);
        }
    }
}

Receiver code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Messaging;

namespace MSMQViewer
{
    class Program
    {
        static void Main(string[] args)
        {
            MessageQueue q = new MessageQueue(@"lab\test");
            q.Formatter = new ActiveXMessageFormatter();
            while (true)
            {
                Message msg = q.Receive();
                Console.WriteLine(msg.Body);
            }
        }
    }
}

Error message:

C:\dev\MSMQTester\MSMQViewer\bin\Debug>MSMQViewer.exe

Unhandled Exception: System.InvalidOperationException: Cannot deserialize the me ssage passed as an argument. Cannot recognize the serialization format. at System.Messaging.ActiveXMessageFormatter.Read(Message message) at System.Messaging.Message.get_Body() at MSMQViewer.Program.Main(String[] args) in c:\dev\MSMQTester\MSMQViewer\Pro gram.cs:line 18

A: 

If you encounter this problem as well, you may consider using the following work around:

System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue(strQueue);
queue.Formatter = new System.Messaging.ActiveXMessageFormatter();
queue.DefaultPropertiesToSend.Priority = System.Messaging.MessagePriority.High;
queue.Send("  Message as string... Not a message object! :(  ");

In the above example, I was able to pass the message while setting the priority to high while avoiding the exception.

Michael Rosario