0

I have a azure function that is triggered every 1 minute. The function creates a MessageReceiver like this

var messageReceiver = new MessageReceiver(serviceBusConnectionString, entityPath, ReceiveMode.PeekLock, null, 0);

The subscription has a lock duration of 1 minute. It then tries to fetch up to 100 messages from the subscription like this:

var allMessages = new List<Message>();
Message message;

do {
    message = await messageReceiver.ReceiveAsync(TimeSpan.FromSeconds(2));
    if (message != null) allMessages.Add(message);
} while (message != null && allMessages.Count() < 100);

After processing the messages are completed using messageReceiver.CompleteAsync and the messageReceiver is closed using messageReceiver.CloseAsync().

The first time the function runs it fetches up to 100 messages from the subscription, but on the next runs it only fetches 0 or 1 message no matter the number of messages available in the subscription (Service Bus Explorer shows that there > 10 messages in the subscription). So it seems that ReceiveAsync returns null even when there is messages available.

Increasing the timeout for ReceiveAsync doesn't seem to help.

Why does ReceiveAsync return null when there is messages available?

jbiversen
  • 371
  • 3
  • 14

1 Answers1

0

I found a solution or workaround. If I change the the service bus topic to not allow partitioning (it requires deleting the topic and creating it again), ReceiveAsync always returns a message when there is a message available.

The problems seems related to this issue Odd Behavior of Azure Service Bus ReceiveBatch()

jbiversen
  • 371
  • 3
  • 14