4

I am connecting to a remote MSMQ within a Windows Service, and doing a BeginReceive as follows:

msmq.ReceiveCompleted += new ReceiveCompletedEventHandler(Process);
msmq.BeginReceive();

The Process method gets the message and calls EndReceive like this:

message = msmq.EndReceive(asyncResult.AsyncResult);

and then processes the message, then calls BeginReceive again like this:

msmq.BeginReceive();

The problem is that for some reason when the MSMQ server reboots, the Process method fires, and gets to the EndReceive line which then throws a MessageQueueException. Once the remote server reboots, no more messages get received and processed until I restart the Windows Service.

It seems odd to me that the ReceiveCompletedEventHandler method (Process) is firing, and also why no more messages get received after the remote server reboots - I'm not quite sure how to ensure that the connection is reestablished after a reboot.

Does anyone know why this is happening? (and how to fix it?).

Note - I've now added some code that handles the case when the EndReceive call throws this specific error, and loops calling BeginReceive() again (with Thread.Sleeps) until there's no error any more. Annoyingly, even though this appears to work when the MSMQ server is back up and BeginReceive seems to work (ie doesn't throw any errors), still NO messages get received any more.

4

2 回答 2

4

我似乎已经解决了这个问题。我已采取以下步骤:

1)我现在已经将我的 BeginReceive 调用移动到一个单独的方法中,该方法循环调用 BeginReceive() 直到不再有异常(在这之间睡 X 秒)。

2) 将 EndReceive 调用包装在 try catch 中,以捕获在 MSMQ 服务器重新启动时调用 ReceiveCompletedEventHandler 并抛出 MessageQueueException 的奇怪情况。

3) 在捕获中,我在消息队列上调用 Close()。这很重要,没有这个,它不起作用,然后我再次调用我的 BeginReceive 方法。

于 2011-05-31T10:27:57.710 回答
0

我似乎有类似的问题,但问题似乎在于底层 msmq 对象已失去与队列的连接。尝试使用 Refresh() 方法进行实验...

于 2011-12-15T17:18:09.833 回答