0
votes

I've an Azure Web Job with the following initialization code

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();
        }
    }
}

The Service Bus queue is configured to have a Lock Duration of 5 minutes, that is the maximum time that Azure allows. The message processing can take more than 30 minutes and the lock renew mechanism works correctly. When the process ends correctly, an exception is thrown 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 and the message goes back to the queue again.

1

1 Answers

0
votes

When you call messsage.Complete() (or CompleteAsync()) then you should instantiate an MessageHandlerOptions object, set AutoComplete to false, and pass it into your message handler registration.

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

For more details, you could refer to this article.