在trye上完成消息的Azure服务总线异常

问题描述 投票:0回答:2

我正在使用GitHub示例来处理主题的消息:

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 });
        }

所有订阅者都成功发送和接收所有消息,但在命令中:

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

始终出现以下错误:

 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

关于如何解决的任何提示?谢谢!

c# azure exception azureservicebus azure-servicebus-topics
2个回答
0
投票

从服务总线队列或主题订阅收到消息时,将返回带有消息的锁定令牌。

锁定令牌可用于删除,死信或延迟消息。

每个队列和主题订阅都具有锁定持续时间属性。根据为锁定持续时间配置的时间跨度,随消息提供的锁定将过期。

在这里,您在完成消息之前正在处理await ProcessEvent(eventName, messageData);

问题必须是在执行_subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);行之前锁定已过期。

在调用ProcessEvent之前增加锁定持续时间或完成消息将解决您的问题。


0
投票

另一个主要原因是默认启用Autocomplete

每当您使用CompleteAsync()时,您还应该实例化一个OnMessageOptions对象,以便将AutoComplete设置为false,并将其传递给OnMessage调用。

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

client.OnMessage(processCalculations, options);

看到这个Similar SO Solution

© www.soinside.com 2019 - 2024. All rights reserved.