如何使用Azure服务总线5.0.0在C#azure函数中手动处理消息完成 [英] How to manually handle message completion in C# azure function with azure service bus 5.0.0

查看:0
本文介绍了如何使用Azure服务总线5.0.0在C#azure函数中手动处理消息完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个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

What有效(基于杰西和乔什的回答)

ServiceBusMessageActions的参数名称必须为messageActions
出于某些原因,不允许更改此名称...

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

推荐答案

您需要使用";MessageActions";作为参数名称,而不是";msgActions";。

这篇关于如何使用Azure服务总线5.0.0在C#azure函数中手动处理消息完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆