I am using the PeekBatch(
method on the QueueClient
(Windows Azure Service Bus package version 2.1.2.0).
It works fine t
The QueueClient is being somewhat helpful. In the Peek methods (Peek and PeekBatch) you can simply call them, or you can give a specific sequence number to retrieve a specific message after a certain sequence number. If you simply call Peek, or in your case PeekBatch, without a sequence number then it will retrieve the very first message, or messages, in the queue. Once the message is returned the QueueClient keeps track of the last sequence number it pulled. Each subsequent call to Peek will fetch the next message in the queue. The idea is that you are "browsing" the messages and not just interested in the first message on the queue every time.
So, if you were in a loop and called peek repeatedly until it didn't return a message you would essentially have browsed all message in the queue.
Since you are calling PeekBatch without a sequence number the QueueClient is remembering the last set it got, then the next call would actually try to get the next set after the last message it browsed. This is why when you recreate the QueueClient it seems to reset. The reason it seemed to reset on it's own after 5 minutes seems odd, but it maybe just cleaning out the browsing values after some point that is tied to the Timeout operation on the queue. By then the sequence number would be pretty far off anyway if it was a busy queue.
If you need to really only look at the very first message then call peek only once. It will only return the first message. If you need to continually pull the first message every time do a Peek(0). If you want the first say 10 messages every time then call PeekBatch(0, 10); that will be like saying give me the first ten messages that have a sequence number greater than 0.
The guidance to reuse the QueueClient is sound. It's doing all sorts of caching of information and things. You don't want to have to recreate it each time.