MaxAutoLockRenewalDuration 不适用于 Azure 服务总线

MaxAutoLockRenewalDuration is not working for Azure Service Bus

我创建了以下示例应用程序来测试“MaxAutoLockRenewalDuration”功能。

这是我的测试场景,

  1. 使用相同的代码创建了两个控制台应用程序。
  2. 在队列中添加一条消息
  3. 运行 接收消息的控制台 App1
  4. 运行 收到相同消息的控制台 App2。

使用以下代码,我将 MaxAutoLockRenewalDuration 设置为 10 分钟。

根据我对“MaxAutoLockRenewalDuration”的理解,它应该自动更新锁直到 10 分钟,第二个控制台应用程序不应收到相同的消息。

public class Program
{
    static string connectionString = "***";
    static string queueName = "firstqueue";
    
    static async Task Main(string[] args)
    {
        try
        {
            //uncomment below if you would like to add message to queue
            //await CreateMessage(queueName, "Message 1 to test 'MaxAutoLockRenewalDuration'");
            await ReceiveMessagesAsync();
        }
        catch (Exception ex)
        {

            throw;
        }
        Console.ReadKey();
    }

    private static async Task CreateMessage(string queueName, string textMessage)
    {
        // create a Service Bus client 
        await using (ServiceBusClient client = new ServiceBusClient(connectionString))
        {
            // create a sender for the queue 
            ServiceBusSender sender = client.CreateSender(queueName);

            // create a message that we can send
            ServiceBusMessage message = new ServiceBusMessage(textMessage);

            // send the message
            await sender.SendMessageAsync(message);
            Console.WriteLine($"Sent a single message to the queue: {queueName}");
        }
    }

    // handle received messages
    static async Task MessageHandler(ProcessMessageEventArgs args)
    {
        string body = args.Message.Body.ToString();
        Console.WriteLine($"Received: {body}");

        System.Threading.Thread.Sleep(TimeSpan.FromMinutes(5));

        // complete the message. messages is deleted from the queue. 
        await args.CompleteMessageAsync(args.Message);
    }

    // handle any errors when receiving messages
    static Task ErrorHandler(ProcessErrorEventArgs args)
    {
        Console.WriteLine(args.Exception.ToString());
        return Task.CompletedTask;
    }

    static async Task ReceiveMessagesAsync()
    {
        var processorOptions = new ServiceBusProcessorOptions
        {
            AutoCompleteMessages = false,
            MaxConcurrentCalls = 1,
            MaxAutoLockRenewalDuration = TimeSpan.FromMinutes(10),
            ReceiveMode = ServiceBusReceiveMode.PeekLock,
            PrefetchCount = 1
        };


        await using (ServiceBusClient client = new ServiceBusClient(connectionString))
        {

            // create a processor that we can use to process the messages
            ServiceBusProcessor processor = client.CreateProcessor(queueName, processorOptions);

            // add handler to process messages
            processor.ProcessMessageAsync += MessageHandler;

            // add handler to process any errors
            processor.ProcessErrorAsync += ErrorHandler;
            
            
            // start processing 
            await processor.StartProcessingAsync();

            Console.WriteLine("Wait for a minute and then press any key to end the processing");
            Console.ReadKey();

            // stop processing 
            Console.WriteLine("\nStopping the receiver...");
            await processor.StopProcessingAsync();
            Console.WriteLine("Stopped receiving messages");
        }
    }
    static async Task ReceiveMessagesAsync(string queueName)
    {
        await using (ServiceBusClient client = new ServiceBusClient(connectionString))
        {
            // create a processor that we can use to process the messages
            ServiceBusProcessor processor = client.CreateProcessor(queueName, new ServiceBusProcessorOptions());

            // add handler to process messages
            processor.ProcessMessageAsync += MessageHandler;

            // add handler to process any errors
            processor.ProcessErrorAsync += ErrorHandler;

            // start processing 
            await processor.StartProcessingAsync();

            Console.WriteLine("Wait for a minute and then press any key to end the processing");
            Console.ReadKey();

            // stop processing 
            Console.WriteLine("\nStopping the receiver...");
            await processor.StopProcessingAsync();
            Console.WriteLine("Stopped receiving messages");
        }
    }
}

我在 Azure.Messaging.ServiceBus 版本“7.1.1”和“7.1.2”中使用您的代码,“MaxAutoLockRenewalDuration”功能运行良好。

这是我的步骤:

1.Send 消息到队列。

2.Run Console App1 收到消息

3.KeepConsole App1运行ning,然后运行Console App2,那里没有收到消息。这是屏幕截图:

但是如果你先运行Console App1收到消息,然后关闭Console App1->然后运行Console App2,就可以看到Console App2 中的消息。这是预期的。