问题描述
如何更新“接收队列消息处理程序”上的锁定? 在事件处理程序上,测试消息不具有续订锁定属性。
Message testMessage;
解决方法
您在上面发布的RenewLock api链接来自旧的不赞成使用的WindowsAzure.ServiceBus nuget包,其中RenewLock方法是BrokeredMessage的一部分。
当前软件包Microsoft.Azure.ServiceBus(您正确使用的)具有RenewLockAsync方法作为接收器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属性来利用自动更新锁定功能。那将在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
};