Search code examples
c#azureazure-functionsmessagingazureservicebus

Azure Function V2 Service Bus Message Deferral


I am attempting to convert my v1 function to a v2 function, but I cannot find a replacement for deferring a message.

In V1 of Azure Functions it was a method on the BrokeredMesage called .DeferAsync(). In V2 there is no longer a BrokeredMessage but just a Microsoft.Azure.ServiceBus.Message and this does not contain the method of .DeferAsync().

According to the docs:

The API is BrokeredMessage.Defer or BrokeredMessage.DeferAsync in the .NET Framework client, MessageReceiver.DeferAsync in the .NET Standard client, and mesageReceiver.defer or messageReceiver.deferSync in the Java client.

But how can I get access to the MessageReciever? Here is an example of my function:

[FunctionName("MyFunction")]
public static void Run([ServiceBusTrigger("topic", "subscription", Connection = "AzureServiceBusPrimary")]Message message, ILogger log)
{
    //Code
}

So does anyone know how to defer a V2 Message that is triggered from the Azure Service Bus?


Solution

  • As you mention, the new message receiver offers an async defer method and you can add this to your function by using the following code:

    [FunctionName("MyFunction")]
    public static async Task Run([ServiceBusTrigger("topic", "subscription", Connection = "AzureServiceBusPrimary")]Message message, string lockToken, MessageReceiver messageReceiver, ILogger log)
    {
        //Your function logic
        await messageReceiver.DeferAsync(lockToken);
    }