尝试消息上的Azure服务总线异常以完成消息

时间:2018-09-04 14:58:44

标签: c# azure exception azureservicebus azure-servicebus-topics

我正在使用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

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

2 个答案:

答案 0 :(得分:0)

从服务总线队列或主题订阅中收到消息时,该消息将返回一个锁定令牌。

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

每个队列和主题订阅都具有“锁定持续时间”属性。根据为锁定持续时间配置的时间跨度,消息随附的锁定将失效。

在这里,您正在完成await ProcessEvent(eventName, messageData);的处理,然后再完成消息。

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

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

答案 1 :(得分:0)

另一个主要原因是默认情况下启用了Autocomplete

无论何时使用CompleteAsync(),都还应实例化一个OnMessageOptions对象,以便将集合AutoComplete设置为false,并将其传递到OnMessage调用中。

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

client.OnMessage(processCalculations, options);

查看此Similar SO Solution