1

I'm using the GitHub example to process the messages of a topic:

private void RegisterSubscriptionClientMessageHandler()
        {
            _subscriptionClient.RegisterMessageHandler(
                async (message, token) =>
                {
                    var eventName = $"{message.Label}{INTEGRATION_EVENT_SUFIX}";
                    var messageData = Encoding.UTF8.GetString(message.Body);
                    await ProcessEvent(eventName, messageData);

                    // Complete the message so that it is not received again.
                    await _subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);
                },
               new MessageHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentCalls = 10, AutoComplete = false });
        }

All messages are sent and received by all subscribers successfully, however in the command:

await _subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);

The following error always occurs:

 ERROR ON ExceptionReceivedHandler EXEPTION: The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue, or was received by a different receiver instance.
Bazinga.EventBus.Bus.EventBusSubscription:Error: ERROR ON ExceptionReceivedHandler EXEPTION: The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue, or was received by a different receiver instance.
- Executing Action: UserCallback

Any tips on how to solve? Thank you!

  • Are you sure the message are still in the queue? The error message says there are no messages in queues – Jayendran Sep 04 '18 at 15:05
  • @Jayendran - Thank you and good question: How can I verify this accurately and effectively? – Daniel Moreira Sep 04 '18 at 15:08
  • I've provided my answer. Additionally, for your question, you can check the Azure portal to check whether your messages were in the queues/topics (or) you can use [Azure Service Bus Explorer](https://stackoverflow.com/a/51750473/7073340) to verify that – Jayendran Sep 04 '18 at 15:26
  • Any update? Did you find the solution useful then upvote/accept the answer,so that it will to help the community – Jayendran Sep 09 '18 at 14:16
  • Sorry for the delay ... none of the solutions posted worked. I still have the same problem. I'm studying the documentation to try to figure out what I'm doing wrong. Keep you informed. Happy Codding ;-) – Daniel Moreira Sep 13 '18 at 13:38

2 Answers2

1

When a message is received from Service Bus Queue or Topic Subscription, a lock token will be returned with the message.

The lock token can be used to delete, dead letter or defer a message.

Every Queue and Topic Subscription has Lock duration property with it. Based on the time span configured for Lock duration, the lock supplied with the message will be expired.

Here, you are doing some processing await ProcessEvent(eventName, messageData); before completing the message.

The problem must be the the lock gets expired before_subscriptionClient.CompleteAsync(message.SystemProperties.LockToken); line is executed.

Increasing the lock duration or completing the message before calling ProcessEvent will solve your problem.

Arunprabhu
  • 1,454
  • 9
  • 15
  • I will test now and return to you. Thanks. – Daniel Moreira Sep 04 '18 at 15:17
  • Hey @arunprabhu can you please explain what is the purpose of the lock duration? in which cases do I need to set it to max (5 min) and how its corresponds to the duration of `ProcessEvent`? if the duration is longer than the process duration? and when is not. – Shahar Shokrani Feb 24 '20 at 11:30
1

The other main reason for this will be Autocomplete is enabled by default.

Whenever you are using CompleteAsync() then you should also instantiate an OnMessageOptions object in order to set the set AutoComplete to false, and pass it into your OnMessage call.

OnMessageOptions onMessageOpt = new OnMessageOptions();
onMessageOpt.AutoComplete = false;

client.OnMessage(processCalculations, options);

See this Similar SO Solution

Jayendran
  • 9,638
  • 8
  • 60
  • 103