i'm reading messages from MessageQueue using PeekCompleted, i do my process here and if everything go right, I need to remove it from the Queue! currenty i am using MyMessageQueue.Receive() and it works, but is this a reliable way of making sure each message will be processed right?
MessageQueue MyMessageQueue;
public Form1()
{
InitializeComponent();
MyMessageQueue = new MessageQueue(@".\private$\Dms");
MyMessageQueue.PeekCompleted += new PeekCompletedEventHandler(MessageQueue_PeekCompleted);
MyMessageQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
MyMessageQueue.BeginPeek();
}
void MessageQueue_PeekCompleted(object sender, PeekCompletedEventArgs e)
{
try
{
Debug.WriteLine("ToProcess:" + e.Message.Body);
//Long process that maybe fail
MyMessageQueue.Receive();
}
finally
{
MyMessageQueue.BeginPeek();
}
}
Receive() receives the first message in the queue, removing it from the queue. What you need is MessageQueue.ReceiveById Method
MyMessageQueue.ReceiveById(e.Message.Id);
Secondly, I think, you always need to call MessageQueue.EndPeek Method
Message m = MyMessageQueue.EndPeek(e.AsyncResult);