I am currently trying to retrieve a specific message from a session.
To do so I use want to use the .Receive(Int64) on the MessageSession where I pass in the sequence number of the message.
Here is my code -
long msgSequenceNr = 1337;
QueueClient queueClient = QueueClient.CreateFromConnectionString(Constants.ServiceBusConnectionString, Constants.TestQueueEntityName, ReceiveMode.PeekLock);
MessageSession msgSession = queueClient.AcceptMessageSession(Constants.TestSessionId);
var peekedMsg = msgSession.Peek(msgSequenceNr); // <-- Works fine!
var receivedMsg = msgSession.Receive(msgSequenceNr); // <-- MessageNotFoundException
Unfortunately the Receive will result in a MessageNotFoundException while the Peek works fine. Is this a limitation that I missed or is there another way to achieve this.
Note that it is possible that there are multiple messages in the session
Receive with the SequenceNumber can only be used in combination with the Defer method. This is how you would implement it:
Sample application: https://code.msdn.microsoft.com/windowsazure/Brokered-Messaging-ccc4f879#content
Update:
Form your comment I noticed that "undeferring" a deferred message could be a solution. Here's some sample code to undefer the message which copies the deferred message to a new message, Completes the deferred message and sends the new message back in the queue. This uses a TransactionScope to transactionally Complete and Resend the message to avoid the risk of losing the message:
Note: here I'm simply taking a copy of the Properties. Keep into account that you might want to copy the Body and any other additional information.