I am writing some code to monitor our azure servicebus queues and topics, I want to notify if we have old messages. My code works except that, if a subscription does not have any active messages in it, then it will hang for one minute and then throw an exception that says:
Unhandled exception. Azure.Messaging.ServiceBus.ServiceBusException: The operation did not complete within the allocated time 00:01:00 for object receiver12. (ServiceTimeout)
---> System.TimeoutException: The operation did not complete within the allocated time 00:01:00 for object receiver12.
Looks like it only happens on session enabled subscriptions. I have tried to first look if there are any messages in the subscription and if so, peek them, but often the messages will be received between I check messages count and the peek so the exception occurs. My monitoring solution does not handle that long timeout. I would just like to retrieve and empty list if the subscription is empty. Have also tried the python implementation and it is the same so there must be something fundamental that I don't get.
Some code:
using Azure.Messaging.ServiceBus;
string connectionString = "Endpoint=sb://xxxxxxxx";
string topicName = "topic";
string subscriptionName = "subscription";
await using var client = new ServiceBusClient(connectionString);
// Exception if subscription does not have any active messages...
ServiceBusSessionReceiver receiver = await client.AcceptNextSessionAsync(topicName, subscriptionName);
// Peek messages
IReadOnlyList<ServiceBusReceivedMessage> peekedMessages = await receiver.PeekMessagesAsync(10);
foreach (ServiceBusReceivedMessage peekedMessage in peekedMessagesFiltered)
{
Console.WriteLine(peekedMessage.EnqueuedTime.ToString());
}