我正在使用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
关于如何解决的任何提示?谢谢!
从服务总线队列或主题订阅收到消息时,将返回带有消息的锁定令牌。
锁定令牌可用于删除,死信或延迟消息。
每个队列和主题订阅都具有锁定持续时间属性。根据为锁定持续时间配置的时间跨度,随消息提供的锁定将过期。
在这里,您在完成消息之前正在处理await ProcessEvent(eventName, messageData);
。
问题必须是在执行_subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);
行之前锁定已过期。
在调用ProcessEvent之前增加锁定持续时间或完成消息将解决您的问题。
另一个主要原因是默认启用Autocomplete。
每当您使用CompleteAsync()时,您还应该实例化一个OnMessageOptions
对象,以便将AutoComplete
设置为false
,并将其传递给OnMessage调用。
OnMessageOptions onMessageOpt = new OnMessageOptions();
onMessageOpt.AutoComplete = false;
client.OnMessage(processCalculations, options);