如何将消息从 Deadletter 主题发送到 Main 并使用 .net core c# 完成它
How to send message from Deadletter topic to Main and complete it using .net core c#
我可以使用以下代码从 Deadletter 向 Main 发送消息,但问题是无法完成该消息。
这一行出错 -
await deadletterReceiver.CompleteAsync(newMessage.SystemProperties.LockToken);
System.InvalidOperationException: Operation is not valid due to the
current state of the object. at
Microsoft.Azure.ServiceBus.Message.SystemPropertiesCollection.ThrowIfNotReceived()
at
Microsoft.Azure.ServiceBus.Message.SystemPropertiesCollection.get_LockToken()
public static async System.Threading.Tasks.Task RunAsync([TimerTrigger("0 */2 * * * *")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
try
{
var deadQueuePath = EntityNameHelper.FormatDeadLetterPath("demo/subscriptions/demo");
MessageReceiver deadletterReceiver = new MessageReceiver(Environment.GetEnvironmentVariable("ConnectionStringSettingName"), deadQueuePath, ReceiveMode.PeekLock,RetryPolicy.Default);
MessageSender sender = new MessageSender(Environment.GetEnvironmentVariable("ConnectionStringSettingName"), "demo",RetryPolicy.Default);
var deadLetter = await deadletterReceiver.ReceiveAsync();
if (deadLetter != null)
{
log.LogInformation($"got new message");
Message newMessage = new Message(deadLetter.Body)
{
ContentType = deadLetter.ContentType,
CorrelationId = deadLetter.CorrelationId
};
//Send the message to the Active Queue
await sender.SendAsync(newMessage);
await deadletterReceiver.CompleteAsync(newMessage.SystemProperties.LockToken); //Unlock the message and remove it from the DLQ
log.LogInformation($"Unlock the message and remove it from the DLQ");
}
}
catch (Exception ex)
{
log.LogInformation($"Exception: {ex}");
}
}
您不应该使用死信消息中的锁定令牌吗?
从本质上改变这行代码:
await deadletterReceiver.CompleteAsync(newMessage.SystemProperties.LockToken); //Unlock the message and remove it from the DLQ
至
await deadletterReceiver.CompleteAsync(deadLetter.SystemProperties.LockToken); //Unlock the message and remove it from the DLQ
我可以使用以下代码从 Deadletter 向 Main 发送消息,但问题是无法完成该消息。
这一行出错 -
await deadletterReceiver.CompleteAsync(newMessage.SystemProperties.LockToken);
System.InvalidOperationException: Operation is not valid due to the current state of the object. at Microsoft.Azure.ServiceBus.Message.SystemPropertiesCollection.ThrowIfNotReceived() at Microsoft.Azure.ServiceBus.Message.SystemPropertiesCollection.get_LockToken()
public static async System.Threading.Tasks.Task RunAsync([TimerTrigger("0 */2 * * * *")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
try
{
var deadQueuePath = EntityNameHelper.FormatDeadLetterPath("demo/subscriptions/demo");
MessageReceiver deadletterReceiver = new MessageReceiver(Environment.GetEnvironmentVariable("ConnectionStringSettingName"), deadQueuePath, ReceiveMode.PeekLock,RetryPolicy.Default);
MessageSender sender = new MessageSender(Environment.GetEnvironmentVariable("ConnectionStringSettingName"), "demo",RetryPolicy.Default);
var deadLetter = await deadletterReceiver.ReceiveAsync();
if (deadLetter != null)
{
log.LogInformation($"got new message");
Message newMessage = new Message(deadLetter.Body)
{
ContentType = deadLetter.ContentType,
CorrelationId = deadLetter.CorrelationId
};
//Send the message to the Active Queue
await sender.SendAsync(newMessage);
await deadletterReceiver.CompleteAsync(newMessage.SystemProperties.LockToken); //Unlock the message and remove it from the DLQ
log.LogInformation($"Unlock the message and remove it from the DLQ");
}
}
catch (Exception ex)
{
log.LogInformation($"Exception: {ex}");
}
}
您不应该使用死信消息中的锁定令牌吗?
从本质上改变这行代码:
await deadletterReceiver.CompleteAsync(newMessage.SystemProperties.LockToken); //Unlock the message and remove it from the DLQ
至
await deadletterReceiver.CompleteAsync(deadLetter.SystemProperties.LockToken); //Unlock the message and remove it from the DLQ