gpt4 book ai didi

azure - 如何在 AzureFunction V2 上的 ServiceBus BrokeredMessage 级别放弃消息或死信消息?

转载 作者:行者123 更新时间:2023-12-03 04:21:23 25 4
gpt4 key购买 nike

我在尝试在 azureFunction 中使用 ServiceBusTrigger 时遇到了一个主要障碍问题。我试图放弃 V2 ServiceBusTrigger 中的服务总线消息或使其成为死信,我该如何执行此操作?我已经尝试了以下解决方案,但我没有得到任何结果。这是我使用的代码示例:

public async static Task Run(Message myQueueItem, TraceWriter log, ExecutionContext context)
{
log.Info($"C# ServiceBus queue trigger function processed message delivery count: {myQueueItem.SystemProperties.DeliveryCount}");

QueueClient queueClient = new QueueClient("[connectionstring]","[queueName]");

////await queueClient.DeadLetterAsync(myQueueItem.SystemProperties.LockToken);
await queueClient.AbandonAsync(myQueueItem.SystemProperties.LockToken);
}

解决方案 1:我尝试像 V1 中那样用 Message myQueueItem 替换 BrokeredMessage,然后我可以在消息控制杆上调用 myQueueItem.Abandon 或死信。但是它返回时有异常:

Microsoft.Azure.WebJobs.Host: Exception binding parameter 'myQueueItem'. System.Private.DataContractSerialization: There was an error deserializing the object of type Microsoft.ServiceBus.Messaging.BrokeredMessage. The input source is not correctly formatted. System.Private.DataContractSerialization: The input source is not correctly formatted."

至少我可以更进一步。到解决方案2.解决方案2:是使用:

 QueueClient queueClient = new QueueClient("[connectionstring]","[queueName]");      
////await queueClient.DeadLetterAsync(myQueueItem.SystemProperties.LockToken);
await queueClient.AbandonAsync(myQueueItem.SystemProperties.LockToken);

我可以使用消息对象中提供的锁,但是,当我尝试使用queueClient发送它时,它说消息从队列中消失了。或不再可用。

有人可以告诉我我是否走在正确的道路上吗?如果我不是,请引导我走上正确的道路。

最佳答案

服务总线消息由 Azure Functions 运行时根据函数调用的成功/失败自动完成或放弃,docs :

The Functions runtime receives a message in PeekLock mode. It calls Complete on the message if the function finishes successfully, or calls Abandon if the function fails.

因此,建议放弃消息的方法是从函数调用中抛出异常。

关于azure - 如何在 AzureFunction V2 上的 ServiceBus BrokeredMessage 级别放弃消息或死信消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48434522/

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