如何使用 azure 服务总线 5.0.0 在 C# azure 函数中手动处理消息完成
How to manually handle message completion in C# azure function with azure service bus 5.0.0
我正在编写一个 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.).
备注:
- Service Bus 是 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)
{
锁仅在接收消息的 AMQP link 上有效。为了手动结算消息,您需要将触发器绑定到 ServiceBusMessageActions
。这些动作可用于消息结算。
例如:
[FunctionName("BindingToMessageActions")]
public static async Task Run(
[ServiceBusTrigger("<queue_name>", Connection = "<connection_name>")]
ServiceBusReceivedMessage[] messages,
ServiceBusMessageActions messageActions)
{
foreach (ServiceBusReceivedMessage message in messages)
{
if (message.MessageId == "1")
{
await messageActions.DeadLetterMessageAsync(message);
}
else
{
await messageActions.CompleteMessageAsync(message);
}
}
}
您需要使用“messageActions”而不是“msgActions”作为参数名称。
我正在编写一个 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.).
备注:
- Service Bus 是 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)
{
锁仅在接收消息的 AMQP link 上有效。为了手动结算消息,您需要将触发器绑定到 ServiceBusMessageActions
。这些动作可用于消息结算。
例如:
[FunctionName("BindingToMessageActions")]
public static async Task Run(
[ServiceBusTrigger("<queue_name>", Connection = "<connection_name>")]
ServiceBusReceivedMessage[] messages,
ServiceBusMessageActions messageActions)
{
foreach (ServiceBusReceivedMessage message in messages)
{
if (message.MessageId == "1")
{
await messageActions.DeadLetterMessageAsync(message);
}
else
{
await messageActions.CompleteMessageAsync(message);
}
}
}
您需要使用“messageActions”而不是“msgActions”作为参数名称。