c# - 消息工作成功完成后 Azure 服务总线“提供的锁无效”
问题描述
我有一个带有以下初始化代码的 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
,消息再次返回队列。
解决方案
当您调用messsage.Complete()
( 或CompleteAsync()
) 时,您应该实例化一个MessageHandlerOptions
对象,将AutoComplete设置为false
,并将其传递到您的消息处理程序注册中。
new MessageHandlerOptions(OnException)
{
AutoComplete = false,
MaxConcurrentCalls = MaxConcurrentCalls, // 1
MaxAutoRenewDuration = MaxAutoRenewDuration // 2 hrs
}
更多细节,你可以参考这篇文章。
推荐阅读
- spring - Envers 停止审计实体
- java - 带有 Hibernate 对象的 Dropwizard Sundial 计划任务
- .net-core - .Net Core 2.1 使用 Where linq 查询搜索数据库时出现 EF Core Null 引用异常
- python - python中的字符串格式化浮点数
- asp.net - 即使文本框有数据,所需的验证也会失败
- image - 图像以 Chrome 为中心但不在 Safari 中
- ios - 您不能直接初始化此类 - 在您的 Storyboard/NIB 文件中,将 Class 类型设置为子类之一
- linux-device-driver - bio_endio 和正确处理 READ 请求
- angular - 使用 [attr.required] 动态添加所需属性后更新 FormControl
- r - 在R中查找列的最大值时如何获取整行