Azure 服务总线:如何更新锁?

Azure Service Bus: How to Renew Lock?

如何更新接收队列消息处理程序上的锁? 在事件处理程序上,测试消息没有更新锁属性。

Message testMessage;

https://docs.microsoft.com/en-us/dotnet/api/microsoft.servicebus.messaging.brokeredmessage.renewlock?view=azure-dotnet

您上面发布的 RenewLock api link 来自旧的已弃用 WindowsAzure.ServiceBus nuget 包,其中 RenewLock 方法是 BrokeredMessage 的一部分。

当前包 Microsoft.Azure.ServiceBus(您正在正确使用)将 RenewLockAsync 方法作为 Receiver https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.servicebus.core.messagereceiver.renewlockasync?view=azure-dotnet 的一部分。您可以从您的 QueueClient 实例调用该方法,例如 queueClient.RenewLockAsync(testMessage)queueClient.RenewLockAsync(message.SystemProperties.LockToken)

但是,您可以通过设置 MessageHandlerOptions 的 MaxAutoRenewDuration 属性 来利用 auto-renew 锁定功能,而不是手动完成这项艰巨的工作。那将在 this example.

中的 RegisterOnMessageHandlerAndReceiveMessages 方法中
        static void RegisterOnMessageHandlerAndReceiveMessages()
        {
            // Configure the MessageHandler Options in terms of exception handling, number of concurrent messages to deliver etc.
            var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
            {
                // Maximum number of Concurrent calls to the callback `ProcessMessagesAsync`, set to 1 for simplicity.
                // Set it according to how many messages the application wants to process in parallel.
                MaxConcurrentCalls = 1,

                // Indicates whether MessagePump should automatically complete the messages after returning from User Callback.
                // False below indicates the Complete will be handled by the User Callback as in `ProcessMessagesAsync` below.
                AutoComplete = false,

                // https://docs.microsoft.com/en-us/azure/service-bus-messaging/message-transfers-locks-settlement#peeklock
                MaxAutoRenewDuration = <some timespan>
            };

            // Register the function that will process messages
            queueClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions);
        }

目前,建议使用 Azure.Messaging.ServiceBus NuGet 包,因为 Microsoft.Azure.ServiceBus 已过时。以下是自动更新处理消息的示例代码:

var client = new ServiceBusClient(connectionString);
var processor = client.CreateProcessor(queueName, new ServiceBusProcessorOptions
{
    MaxAutoLockRenewalDuration = TimeSpan.FromHours(100),
});
processor.ProcessMessageAsync += async arg =>
{
    //process your message
    await Task.Delay(Timeout.Infinite);
};
processor.ProcessErrorAsync += async arg =>
{
    //process errors
};