1
votes

I am encountering one major road block issue when trying to use ServiceBusTrigger in azureFunction. I am trying to abandon, or deadletter, a service bus message in V2 ServiceBusTrigger, How can I do so? I've tried the following solution, but I didn't get anywhere. Here is the codeSample I used:

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);
 }

Solution 1: I tried to substitute Message myQueueItem for BrokeredMessage like in V1, I then can call myQueueItem.Abandon, or deadletter, on the message lever. However It came back with exception:

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."

At least I can go one step further. to solution 2. Solution 2: is to use:

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

I can use the lock provided in the Message Object, however, when I try to send it with queueClient, It said the message gone from the queue. or no longer available.

Can anybody let me know if i am on the right track? If I am not, please kindly guide me in the right track.

1

1 Answers

0
votes

Service Bus messages are automatically completed or abandoned by Azure Functions runtime based on the success/failure of the function call, 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.

So, the suggested way to Abandon your message is to throw an exception from function invocation.