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.