Remote MSMQ connection apparently lost problem when MSMQ server reboots
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 pro开发者_如何学JAVAblem 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.
I seem to have fixed the problem. I've taken the following steps:
1) I've now moved my BeginReceive call into a separate method, which loops around calling BeginReceive() until there are no exceptions any more (sleeping for X seconds in between).
2) Wrapped the EndReceive call in a try catch, to catch the odd case where the ReceiveCompletedEventHandler is called when the MSMQ server is rebooted, and throws a MessageQueueException.
3) In the catch, I call Close() on the Message Queue. This is important, without this, it didn't work, then I call my BeginReceive method again.
I Seem to have similar problem but the problem seems to lie in the fact that the underlying msmq object has lost the connection to the queue. Trying to laborate with the Refresh() method...
精彩评论