1

我有一个简单的控制台应用程序,我使用 MSMQ 在写入 SQL Server 之前存储消息。我是 MQ 新手,如有错误请见谅。基本上,没有抛出错误,代码直接运行而无需进入处理程序......任何帮助/指导都非常感谢......代码如下,谢谢:-

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();
    }
4

1 回答 1

2

好的,对于我们中间的新手......我很少使用控制台应用程序来测试东西,但我在这里。控制台应用程序正在结束,因此线程有时会启动,有时会完成,但没有任何东西可以返回到控制台,因为我没有告诉它保持打开状态。做那件事Console.ReadKey();

于 2013-07-05T09:28:40.427 回答