locked
problem retrieving the Body information of the MSMQ message

    Question

  • I'm trying to use MSMQ to send and receive message.  Below is the sample code I have, under a button_click event.

    -----------------------
                string strDestQ = @".\private$\WANSqueue";
                try
                {
                    //create the queue if needed
                    if (!MessageQueue.Exists(strDestQ))
                        MessageBox.Show(strDestQ + " doesn't exist");

                    MessageQueue mq = new MessageQueue(strDestQ);
                    mq.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] {typeof(Message)});
                    //build message
                    Message m = new Message();
                    m.Label = "Hello World";
                    m.Body = "Hello World";

                    // send and close queue
                    mq.Send(m);
                    MessageBox.Show("Message sent!");

                    // receive message synchronously
                    Message messageReceived = new Message();
                    messageReceived = mq.Receive();
                    mq.Close();

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
    ---------------------------


    No problem with creating queue and sending message.  But I get a System.InvalidOperationException: There was an error reflecting type 'System.Messaging.Message' error at the mq.Receiver() statement.

    It appears the mq.Receive() ran and got the messageReceivedLabel.  The problem is with messageReceivedBody.  I confirmed that the Body of the message sitting in the queue before the Receive() statement is in XML format (default). 

    What do I need to do the receive the Body of the message correctly?

    Delphine

    Thursday, January 15, 2009 6:18 PM

Answers

  •  

    Hello,

    Welcome to MSDN Forums!

    From your code snippet, the body of the message you sent is a string object.   The message body has been serialized into stream when the message is sent.   So when receiving the message, we can actually deserialize the message body stream back to string object.  

    Based on above analyze, the MessageQueue.Formatter should be set to format the object whose type if string and not typeof Message.   Therefore, I guess you code snippet can be modified as below to fulfill your target.

            private MessageQueue mq;

     

            private void button1_Click(object sender, EventArgs e)

            {

                string strDestQ = @".\private$\WANSqueue";

                try

                { 

                    //create the queue if needed

                    if (!MessageQueue.Exists(strDestQ))

                    {

                        MessageBox.Show(strDestQ + " doesn't exist");

                        mq = MessageQueue.Create(strDestQ);

                    }

                    else

                        mq = new MessageQueue(strDestQ);

     

                    mq.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });

                    //build message

                    System.Messaging.Message m = new System.Messaging.Message();

                    m.Label = "Hello World";

                    m.Body = "Hello World";

     

                    // send and close queue

                    mq.Send(m);

                    MessageBox.Show("Message sent!");

     

                    // receive message synchronously

                    System.Messaging.Message messageReceived = new System.Messaging.Message();

                    messageReceived = mq.Receive();

                    string body = mq.Formatter.Read(messageReceived) as string;

                    MessageBox.Show(messageReceived.Label + " " + body);

     

                }

                catch (Exception ex)

                {

                    MessageBox.Show(ex.ToString());

                }

                finally

                {

                    mq.Close();

                }

            }

     

    If you have any questions, please be free to let me know.

     

    Best Regards,
    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. http://forums.msdn.microsoft.com/en-US/csharpide/thread/8e9ed0d7-11ff-402a-8489-9b5f05eeb706 http://forums.msdn.microsoft.com/en-US/vssetup/thread/60424309-bd78-4ca2-b618-03c4a16123b6
    Wednesday, January 21, 2009 3:36 AM