Reputation: 6354
I am trying to write some unit tests to verify all of my queue operations are working as expected, but I have run into the strangest situation:
I have the following code in my [TestInitialize] method:
var ns = NamespaceManager.CreateFromConnectionString(config.ServiceBusConnectionString);
var queueDescription = ns.GetQueue("happy-birthday");
Client = QueueClient.CreateFromConnectionString(config.QueueConnectionString, ReceiveMode.ReceiveAndDelete);
if (queueDescription.MessageCount > 0)
{
while (Client.Peek() != null)
{
var msg = Client.Receive();
msg.Complete();
}
}
My queue has a few Active Messages (confirmed with the queueDescription object) and the Azure portal confirms there should be two active messages that should be "completed" by the code above. However, Client.Receive() just stalls the code for a 30 second wait, then returns null.
I do not understand why the Client.Peek() returns the message, but when I called Client.Receive() i get a null returned.
Upvotes: 0
Views: 406
Reputation: 6354
I identified the problem was due to my assumption of what "Deferred" means.
I assumed deferred was the way I en-queued the same message back into the queue, when in fact deferred messages are set aside, and must be processed directly by retrieving the message by sequence number.
I was able to retrieve the message following these steps:
this was the way I was able to get the message queue empty.
NameSpace = NamespaceManager.CreateFromConnectionString(ConnectionString);
var queueInfo = NameSpace.GetQueue("happy-birthday");
Client = QueueClient.CreateFromConnectionString(connectionString, "happy-birthday");
if (queueInfo.MessageCount > 0)
{
var message = Client.Peek();
while (message != null)
{
if (message.State == MessageState.Deferred)
{
message = Client.Receive(message.SequenceNumber);
}
else
{
message = Client.Receive();
}
message.Complete();
message = Client.Peek();
}
}
Upvotes: 1