PeekCompleted not firing when messages are received in the queue

时间:2015-05-24 21:21:02

标签: msmq system.messaging

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 个答案:

答案 0 :(得分:0)

我在这里找到了答案: MSMQ ReceiveCompleted not firing?

基本上,控制台应用程序在事件有机会发生之前就已经结束了。

我放了一个Console.ReadKey();在主要功能的最后,一切都很顺利。