1

I'm using isolated azure function to receive message from the queue. I need to validate received message and if it invalid, send it to the dead-letter queue. The only way how to do it I found is to throw exception and after 10 retries the message will be moved to dead letter queue. Of course it is not good solution. Maybe anyone faced the same task? Thanks!

 [Function("Example")]
 public async Task ExampleAsync([ServiceBusTrigger("example", Connection = "ServiceBusConnection")] string entityId)
 {
     if (!int.TryParse(entityId, out var id))
     {
         // TODO: Move to dead-letter queue
     }
 }  
Lev Kostychenko
  • 147
  • 1
  • 10

1 Answers1

2

If you're using the in-process worker, you can dead-letter by binding to ServiceBusMessageActions in your function and then calling its DeadLetterMessageAsync method. This would also require that you bind to ServiceBusMessage rather than just string entityId.

For example:

[FunctionName("BindingToMessageActions")]
public static async Task Run(
    [ServiceBusTrigger("<queue_name>", Connection = "<connection_name>")]
    ServiceBusReceivedMessage message,
    ServiceBusMessageActions messageActions)
{
    if (!int.TryParse(message.Body.ToString()), out var id))
    {
        await messageActions.DeadLetterMessageAsync(message);
    }
}

More examples can be found in the package README.

Unfortunately, this is not yet available in the isolated process model.

Jesse Squire
  • 6,107
  • 1
  • 27
  • 30
  • Yes, I found this example but as you mentioned - it is not available in isolated process – Lev Kostychenko Jul 28 '23 at 09:29
  • Ah, my bad. I missed "isolated" in your question and was covering all bases. In the case of an isolated function, you cannot manually complete a message received by the trigger at this time. The infrastructure needed to do so is under development, but there's no firm timeline to share yet. – Jesse Squire Jul 28 '23 at 13:35
  • I get this error when using the above code `System.Private.CoreLib: Exception while executing function: Functions.MyFunction. System.Private.CoreLib: Result: Failure Exception: Microsoft.Azure.Functions.Worker.FunctionInputConverterException: Error converting 1 input parameters for Function 'MyFunction': Cannot convert input parameter 'message' to type 'Azure.Messaging.ServiceBus.ServiceBusReceivedMessage' from type 'System.String'. ` It seems only a message string is supported as input. – goku_da_master Aug 07 '23 at 17:51
  • Binding to `ServiceBusReceivedMessage` is available in the preview packages for the isolated worker and in v5.10 or above of the Service Bus extensions package. – Jesse Squire Aug 07 '23 at 18:00
  • Getting closer but now it's erroring on the ServiceBusMessageActions parameter with the same error: `Error converting 1 input parameters for Function 'MyFunction': Cannot convert input parameter 'messageActions' to type 'Microsoft.Azure.WebJobs.ServiceBus.ServiceBusMessageActions' from type 'System.String'` I'm using Microsoft.Azure.WebJobs.Extensions.ServiceBus 5.11. Updating to the latest package got rid of the error for the ServiceBusReceivedMessage parameter. – goku_da_master Aug 07 '23 at 20:26
  • As mentioned above, message actions can't be used in the isolated process currently. – Jesse Squire Aug 07 '23 at 21:08
  • Until that's implemented what's our option? Use QueueClient? – goku_da_master Aug 08 '23 at 18:16
  • 1
    Until that is implemented, you'll want to stick with the in-process model if you need to use the trigger and directly complete messages. A Service Bus message can only be completed on the same AMQP link that it was read from. You won't be able to receive a message from the trigger and then pass it to another `ServiceBusReceiver` instance to complete it. I'd also note that the `QueueClient` type that you've mentioned is from the deprecated `Microsoft.Azure.ServiceBus` package. We'd advise against using that for new development. – Jesse Squire Aug 09 '23 at 13:25