gpt4 book ai didi

c# - 如何使用 .net core c# 将消息从 Deadletter 主题发送到 Main 并完成它

转载 作者:行者123 更新时间:2023-12-03 05:00:51 24 4
gpt4 key购买 nike

我可以使用下面的代码从 Deadletter 向 Main 发送消息,但问题无法完成该消息。

这一行出错 -等待 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}");
}
}

最佳答案

您不应该使用死信消息中的锁定 token 吗?

本质上改变这行代码:

 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

关于c# - 如何使用 .net core c# 将消息从 Deadletter 主题发送到 Main 并完成它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57995489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com