Using an Azure ServiceBus Queue with sessions (message-ordering) enabled, I have sessions that need to last between a couple of minutes and a couple of hours.
To that end, I have configured my QueueClient as follows:
_options = new SessionHandlerOptions(ExceptionReceivedHandler)
{
AutoComplete = false,
MaxConcurrentSessions = 50,
MessageWaitTimeout = TimeSpan.FromSeconds(30),
};
and start receving messages as follows:
_queueClient.RegisterSessionHandler(ProcessSessionMessagesAsync, _options);
After a few (between 1 and 6) successful (and near instantaneous) message-receive callbacks -- both for new sessions and existing ones, the receive handler just stops firing. Using ServiceBusExplorer
, I can see messages sitting on the servicebus queue. Interestingly, they all have a DeliveryCount=1.
After some time (this varies between a few seconds and a few minutes -- but not a multiple of MessageWaitTimeout), I start receiving a trickle of messages again. If I restart the receiver, then I sometimes get a burst of all remaining messages, sometimes nothing more.
I've tried various values for MessageWaitTimeout
, and although lower values seem to lessen the issue, the delays still exist.
Interesting, if I complete sessions after every message received, the problem still persists.
Has anyone experienced anything like this? The behavior is so infuriatingly inconsistent...
FWIW, my messageReceivedHander
looks something like this:
async Task ProcessSessionMessagesAsync(IMessageSession session, Message message, CancellationToken token)
{
try
{
var myEvent = Serializer.Deserialize(message.Body);
await _receiveCallback(Subscription, myEvent);
await session.CompleteAsync(message.SystemProperties.LockToken);
// Drop the session after every message (**makes no difference**)
await session.CloseAsync();
}
catch (Exception ex)
{
await session.AbandonAsync(message.SystemProperties.LockToken);
}
}