Search code examples
c#azureazure-functionsazureservicebus.net-6.0

Perform MessageActions on Messages for .Net 6 Isolated Worker ServiceBusTriggers


I want to be able to perform flexible actions on Service Bus Messages through a .Net 6 out-of-process ServiceBusTrigger. Actions include abandoning, completing, deferring, as well as dead-letter-queuing messages. I.e. the same actions that are available through the ServiceBusReceivedMessage and ServiceBusMessageActions bindings of the in-process ServiceBusTrigger.

However, out-of-process triggers seem only to be able to bind the message body as a string, as well as the FunctionContext. Are the described actions available for out-of-process triggers in some other way? If not, do you know if it's on the roadmap for near future releases?

Example of how the actions are available for in-process triggers:

[FunctionName(nameof(InProcessReceiver))]
public async Task RunAsync(
   [ServiceBusTrigger("%TOPIC_NAME%", "%SUB_NAME%", Connection="CONNECTION_NAME")] 
   ServiceBusReceivedMessage message,
   ServiceBusMessageActions messageActions
)
{
   await messageActions.DeadLetterMessageAsync(message);
   //await messageActions.AbandonMessageAsync(message);
   //await messageActions.CompleteMessageAsync(message);
   //await messageActions.DeferMessageAsync(message);
}

Example of out-of-process trigger, for which I want to do the same things as above

[Function(nameof(OutOfProcessReceiver))]
public async Task RunAsync(
   [ServiceBusTrigger("%TOPIC_NAME%", "%SUB_NAME%", Connection="CONNECTION_NAME")] 
   string mySbMsg
)
{   
   //How to access message actions and bind them to the current message here?          
}

As mentioned by the Microsoft team in this roadmap blog post, the future is isolated. Therefore I'm trying to avoid in-process, but I realize that it might be the only solution for now.

Thanks in advance!


Solution

  • Update: as of version 5.14.0, ServiceBusMessageActions can be injected into the functions for message settlement operations. A bug with dead-lettering was fixed in 5.14.1

    Example:

    public async Task Run(
       [ServiceBusTrigger("queue", Connection = "ServiceBusConnection")]
       ServiceBusReceivedMessage message,
       ServiceBusMessageActions messageActions)
    {
     _logger.LogInformation("Message ID: {id}", message.MessageId);
     _logger.LogInformation("Message Body: {body}", message.Body);
    
     await messageActions.DeadLetterMessageAsync(message);
    }
    

    Original

    Are the described actions available for out-of-process triggers in some other way? If not, do you know if it's on the roadmap for near future releases?

    At the moment it's not possible. Out of process/isolated worker SDK cannot pass to the Function native SDK types. There are a few related issues you can track for updates.