0
votes

I created a couple of test applications to test out MSMQ. I wanted to test out using MSMQ in a couple of console applications before I do any real work.

MSMQ is installed on a local server in workgroup mode. I have MSMQ installed on my desktop server as well. I am trying to send and receive messages from a remote private queue.

I am able to send messages to MSMQ just fine. I can see them in the queue. However, when I try to read them out, the PeekCompleted event never fires. I wrote another application that calls the synchronous version of Receive and I can retrieve the messages. For some reason BeginPeek and PeekCompleted aren't working for me.

Can I not use BeginPeek with a workgroup installation? If I can use BeginPeek with a workgroup installation, does anybody know what's wrong?

class Program
{
    static void Main(string[] args)
    {
        try
        {
            MessageQueue mq = new MessageQueue("FormatName:Direct=TCP:10.1.1.102\\private$\\EmailQueue");
            mq.Formatter = new XmlMessageFormatter(new Type[] { typeof(Email) });
            mq.PeekCompleted += new PeekCompletedEventHandler(ProcessMessage);
            Console.WriteLine("Begin listening on queue...");
            mq.BeginPeek();
            return;
        }catch(Exception e){
            Console.WriteLine(e.ToString());
        }

    }

    private static void ProcessMessage(Object source, PeekCompletedEventArgs asyncResult)
    {
        Console.WriteLine("Recieveing message...");

        MessageQueue mq = (MessageQueue)source;
        mq.EndPeek(asyncResult.AsyncResult);
        Message m = mq.Receive();
        Email e =(Email) m.Body;
        Console.WriteLine("Email Message:");
        Console.WriteLine(e);
        mq.BeginPeek();

        return;            
    }
}
1

1 Answers

0
votes

I found my answer here: MSMQ ReceiveCompleted not firing?

Essentially, the console application was ending before the event had a chance to fire.

I put a Console.ReadKey(); at the end of the Main function and all is well.