We've got a number of in-process azure functions that have bindings along the lines of:
public async Task Run(
[ServiceBusTrigger("%TriggerQueueName%", Connection = "TriggerQueueConnection")] InitMessageBody messageBody, string correlationId, ILogger log,
[ServiceBus("%TriggerQueueName%", Connection = "TriggerQueueConnection", EntityType = ServiceBusEntityType.Queue)] ServiceBusSender originatingQueueSender,
[ServiceBus("%OutputQueueName%", Connection = "OutputQueueConnection", EntityType = ServiceBusEntityType.Queue)] ServiceBusSender outputQueueSender
)
We use the ServiceBusSender instances rather than Output binding as it gives us easy control over batch sending (in the case of the output sender) but we also have an exponential back-off retry handler pattern we use to schedule a copy of the original message back onto the originating queue when a transient error occurs.
I've got some more functions to write and now that isolated is the preferred (and currently the only available one for .net8) model for Azure Functions, I thought I'd give it a go but I can't work out how to implement a similar strategy to using the ServiceBusSender which I don't believe is available as a binding in v5.x functions.
For output batching, from what I can tell, while you can send multiple messages on the Output by have the return type set as T[], you can only supply the message body and have no control over metadata like messageId or correlationId, which I want.
For scheduling messages back onto the originating queue, the only way I can see is to spin up a ServiceBusClient in the function ctor and use that to generate a ServiceBusSender.
Is the only/correct way of doing what I want do to spin up/down clients and senders manually?
If this is the correct way, how should disposal of the ServiceBusClient be handled? Am I best off using DI to provide the client and let that take care of disposal when appropriate (I do this already for web-app integration but unsure on how best to do this given the functions lifecycle)?
Feeling like there's a lot of grind appearing that I guess was taken care of by the in-process model - pondering if I should drop back down to .Net6 until the in-process model becomes available for .NET8.
I tried the sample code below in .NET 8 isolated model to send a message from the input queue and receive it in the output queue with ServiceBusClient, using the Service Bus Queue Trigger function.
Code :
Function1.cs :
Program.cs :
local.settings.json :
Output :
The following function started running as shown below.
Next, I sent a message to the input queue (kamqueue) in the Azure Service Bus, as shown below.
I received the above input queue message in the output queue in the Azure Portal.
I successfully sent the message from the input queue(kamqueue) and received it in the output queue.