Brandon
Brandon

Reputation: 183

How to intentionally deadletter a message is azure functions v2

This question is similar to others out there with answers, but I think things may have changed with AF v2 (I'm jumping into v2 without v1 experience). It seems that the assembly that AF v2 is integrated with for interacting with ServiceBus is Microsoft.Azure.ServiceBus, which has a class called "Message", which is not quite the same as "BrokeredMessage" found in other microsoft servicebus assemblies. One key difference between the two is that BrokeredMessage (which is referenced in almost all of the documentation, examples, and any other thread out there that I can find) has a .Deadletter() method on it, while Message does not. How can I deadletter a message intentionally when I don't have access to the Client or Receiver that received it?

(I have seen suggestions out there related to just cloning the message, putting on my own "dead letter" queue, and letting AF commit the original - I do not feel this is an adequate solution.)

Upvotes: 3

Views: 1411

Answers (2)

Amicable
Amicable

Reputation: 3101

This was a real pain to find out as it's still not documented. The API reference on the Azure SDK for .NET project readme even points to the documentation from the old namespace 🤦‍♂️

I had to search through the source code to find the correct way to get the LockToken.

    [FunctionName("MyFunction")]
    public static async Task Run([ServiceBusTrigger("myqueue", Connection = "myconnectionstring")] Message message,
        ILogger log, MessageReceiver messageReceiver)
    {
        await messageReceiver.DeadLetterAsync(message.SystemProperties.LockToken);
    }

Putting MessageReceiver as a parameter with automatically bind it, no additional set up needed.

Upvotes: 4

Brandon
Brandon

Reputation: 183

I have yet to prove it out in the functions environment, but according to the ticket I opened on github (https://github.com/Azure/azure-webjobs-sdk/issues/1986), webjobs v3 supports binding both the Message and MessageReceiver that received it in the function trigger, and then the MessageReceiver can be used to deadletter the message.

Upvotes: 5

Related Questions