gpt4 book ai didi

c# - 如何使用 azure 服务总线 5.0.0 在 C# azure 函数中手动处理消息完成

转载 作者:行者123 更新时间:2023-12-02 22:49:51 24 4
gpt4 key购买 nike

我正在编写一个 Azure 函数来获取 Azure 服务总线中的消息。我想手动处理任何异常 ("autoCompleteMessages": false)无法弄清楚如何将完整或放弃发送回服务队列。

尝试了选项 1:

[FunctionName("SBQ_F1_VC")]
public static async Task Run([ServiceBusTrigger("sbqfn1", Connection = "BrnlTest1_SERVICEBUS")]
ServiceBusReceivedMessage msg, ILogger log)
{
//.....

if(!Int32.TryParse(msg.ApplicationProperties.GetValueOrDefault("vid").ToString(), out vid))
{ await using ServiceBusClient client = new ServiceBusClient(Environment.GetEnvironmentVariable("BrnlTest1_SERVICEBUS"));
ServiceBusReceiver msgRcvr = client.CreateReceiver(Environment.GetEnvironmentVariable("queueName"), new ServiceBusReceiverOptions());
//await msgRcvr.RenewMessageLockAsync(msg);
await msgRcvr.AbandonMessageAsync(msg); //vid = 0;
}

//.....
}

错误选项 1

System.Private.CoreLib: Exception while executing function: SBQ_F1_VC. Azure.Messaging.ServiceBus: 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. (MessageLockLost).

尝试了选项 2:

[FunctionName("SBQ_F1_VC")]
public static async Task Run([ServiceBusTrigger("sbqfn1", Connection = "BrnlTest1_SERVICEBUS")]
ServiceBusReceivedMessage[] msgs,
ServiceBusMessageActions msgActions)
{
//.....
await msgActions.DeadLetterMessageAsync(msg);

}

错误选项 2:

Microsoft.Azure.WebJobs.Host: Error indexing method 'SBQ_F1_VC'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'msgActions' to type ServiceBusMessageActions. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).

注释:

  • 服务总线是 5.0.0,因此我必须使用 Azure.Messaging.ServiceBus 命名空间
    • 这意味着消息对象是 ServiceBusReceivedMessage
<小时/>

什么有效(基于 Jesse 和 Josh 的答案)

ServiceBusMessageActions 的参数名称​​必须messageActions
由于某种原因,更改此名称是不被容忍的...

public static async Task Run(
[ServiceBusTrigger("sbqfn1", Connection = "BrnlTest1_SERVICEBUS")]
ServiceBusReceivedMessage[] msgs,
ServiceBusMessageActions messageActions)
{

最佳答案

您需要使用“messageActions”作为参数名称,而不是“msgActions”。

关于c# - 如何使用 azure 服务总线 5.0.0 在 C# azure 函数中手动处理消息完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68350196/

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