我正在使用下面的代码尝试从订阅的死信队列中删除特定消息。我正在使用 PeekBySequenceNumberAsync 来获取特定消息。问题是该方法显然没有设置锁定令牌。我收到以下错误:“提供的锁无效。锁已过期,或者消息已从队列中删除,或者由不同的接收器实例接收。”
关于如何从订阅死信队列中删除特定消息的任何想法?我正在使用 .net 核心 Microsoft.Azure.ServiceBus 库。
public async Task<bool> DeleteMessage(long sequenceNumber, string topicPath, string subscriptionName, bool deadLettered = false)
{
bool success = false;
string connectionString = Environment.GetEnvironmentVariable("SB_CONNECTION_STRING");
MessageReceiver receiver = null;
try
{
string path = EntityNameHelper.FormatSubscriptionPath(topicPath, subscriptionName);
if (deadLettered)
path = EntityNameHelper.FormatDeadLetterPath(path);
receiver = new MessageReceiver(connectionString, path, ReceiveMode.PeekLock );
var message = await receiver.PeekBySequenceNumberAsync(sequenceNumber);
// If we have found the message
if (message != null)
{
await receiver.CompleteAsync(message.SystemProperties.LockToken);
success = true;
}
else
{
Console.WriteLine("Message with sequence number: " + sequenceNumber.ToString() + " was not found");
}
}
catch (ServiceBusException e)
{
if (!e.IsTransient)
{
Console.WriteLine(e.Message);
}
}
finally
{
if (receiver!=null)
await receiver.CloseAsync();
}
return success;
}