【问题标题】:Azure Service Bus "The lock supplied is invalid" after message work successfully completed消息工作成功完成后 Azure 服务总线“提供的锁无效”
【发布时间】:2019-05-30 06:17:54
【问题描述】:

我有一个带有以下初始化代码的 Azure Web 作业

class Program
{
    static void Main(string[] args)
    {
        IHostBuilder builder = new HostBuilder()
        .ConfigureWebJobs(b =>
        {
            b.AddServiceBus((opt) =>
            {
                opt.ConnectionString = "Connection string";
                opt.MessageHandlerOptions = new MessageHandlerOptions((ex) =>
                {
                    return Task.Run(() =>
                    {
                        // logging the error message
                    });
                })
                {
                    MaxAutoRenewDuration = new TimeSpan(0, 0, 5, 0),
                    MaxConcurrentCalls = 1
                };
            });
        })
        .UseConsoleLifetime();

        IHost host = builder.Build();
        using (host)
        {
            host.Run();
        }
    }
}

服务总线队列配置为具有 5 分钟的锁定持续时间,这是 Azure 允许的最长时间。 消息处理可能需要 30 多分钟,并且锁更新机制正常工作。 当进程正确结束时,抛出异常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,消息再次返回队列。

【问题讨论】:

    标签: c# azure-webjobs servicebus


    【解决方案1】:

    当您调用messsage.Complete()(或CompleteAsync())时,您应该实例化一个MessageHandlerOptions 对象,将AutoComplete 设置为false,并将其传递到您的消息处理程序注册中。

    new MessageHandlerOptions(OnException)
    {
        AutoComplete = false,
        MaxConcurrentCalls = MaxConcurrentCalls, // 1
        MaxAutoRenewDuration = MaxAutoRenewDuration // 2 hrs
    }
    

    更多详情可以参考这个article

    【讨论】:

    • 我正在使用 Microsoft.Azure.WebJobs SDK v3.0.8 和 Microsoft.Azure.ServiceBus v3.4.0 和 Microsoft.Azure.WebJobs.Extensions.ServiceBus v3.0.5。在这些版本的 SDK 中,message.Complete() 方法不再存在。
    猜你喜欢
    • 1970-01-01
    • 2022-01-16
    • 2020-08-30
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 2019-02-09
    相关资源
    最近更新 更多