MSMQ ReceiveCompleted不触发? [英] MSMQ ReceiveCompleted not firing?
问题描述
我有一个简单的控制台应用程序,在写入SQL Server之前,我正在使用MSMQ来存储消息.我是MQ新手,所以如果有一个愚蠢的错误,请原谅我.基本上,不会抛出任何错误,并且代码无需处理程序即可直接运行...非常感谢任何帮助/指导...代码如下,谢谢:-
I have a simple console app that I'm using MSMQ to store messages before writing to SQL Server. I'm an MQ newbie so please forgive me if there's a stupid error. Basically, there is no error thrown and the code just runs straight through without going into the handler... Any help / guidance hugely appreciated... code as follows, thanks:-
public void MSMQ_GetMessage(string _MQ_Path)
{
//set the correct message queue
MessageQueue _msgQ = new MessageQueue(_MQ_Path, QueueAccessMode.ReceiveAndAdmin);
//set the format of the message queue
// _msgQ.Formatter = new XmlMessageFormatter(new Type[] { typeof(_TwitterStreamFeed) });
_msgQ.ReceiveCompleted += new ReceiveCompletedEventHandler(_msgQ_RecieveCompleted);
_msgQ.Formatter = new BinaryMessageFormatter();
_msgQ.BeginReceive();
}
//method to process message
public void _msgQ_RecieveCompleted(object sender, ReceiveCompletedEventArgs e)
{
//queue that have received a message
MessageQueue _mq = (MessageQueue)sender;
try
{
//get the messge off the queue
Message _mqmsg = _mq.EndReceive(e.AsyncResult);
//set the values back into a formatted struct
_TwitterStreamFeed _ts = (_TwitterStreamFeed)_mqmsg.Body;
//now process your SQL....
_azuresql.writeMessageToStorage(_ts);
}
catch
{
}
//refresh queue just in case any changes occurred (optional)
_mq.Refresh();
//tell MessageQueue to receive next message when it arrives
_mq.BeginReceive();
}
推荐答案
好,对于我们中间的新手来说...我很少使用控制台应用程序来测试东西,但是我在这里.控制台应用程序正在结束,因此线程有时会启动,有时会完成,但是由于我没有告诉它保持打开状态,因此无法返回到控制台任何内容.为此,只需执行Console.ReadKey();
OK, for the novices among us... I rarely use a console app to test stuff out but I have here. The console app was ending and hence the threads sometimes started, and sometimes completed but nothing could be returned to the console as I hadn't told it to remain open. to do that just do Console.ReadKey();
这篇关于MSMQ ReceiveCompleted不触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!