成功完成消息工作后,Azure服务总线“提供的锁无效”

时间:2019-05-30 06:17:54

标签: c# azure-webjobs servicebus

我有一个带有以下初始化代码的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();
        }
    }
}

Service Bus队列配置为具有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异常,并且消息再次返回队列。

1 个答案:

答案 0 :(得分:0)

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

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

有关更多详细信息,您可以参考此article