1

Is there a way you output multiple Brokered Messages to an Azure Service Bus in Azure Functions? by default you can output a single Brokered Message but not multiple.

Currently using the SDK to do this but wondered if there is a way you can do this using the output...

Thanks

2 Answers2

4

As per the documentation on ServiceBus output bindings:

For creating multiple messages in a C# function, you can use ICollector<T> or IAsyncCollector<T>. A message is created when you call the Add method.

Here is a simple example of using ICollector (also directly from the docs):

public static void Run(TimerInfo myTimer, TraceWriter log, ICollector<string> outputSbQueue)
{
    string message = $"Service Bus queue message created at: {DateTime.Now}";
    log.Info(message); 
    outputSbQueue.Add("1 " + message);
    outputSbQueue.Add("2 " + message);
}

I personally find that all of the supported input/output bindings are well documented and examples are readily available at the link I've shown here. Just pick the appropriate binding you're working with (if it's something other than Service Bus)

Jesse Carter
  • 20,062
  • 7
  • 64
  • 101
  • Tiny nitpick - you should prefer the asynchronous version. Especially with IO-bound operations, which Azure Service Bus sends are. – Sean Feldman Jul 22 '17 at 04:41
  • 2
    I find the input/output bindings to be quite poorly documented. For instance, if it wasn't an example used in the docs, how would one know that there was an overload that accepted the TimerInfo object? Where are the various Run overloads listed? – Tim Jun 17 '19 at 18:32
  • I agree they are documented poorly and need more examples. This answer also doesn't make clear how you bind this. What are the attributes to bind the ICollector to the ASB queue? – IronSean Dec 09 '21 at 21:29
  • @IronSean You mean this? https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus-output?tabs=csharp#attributes-and-annotations – Jesse Carter Dec 10 '21 at 19:38
  • @JesseCarter that pages currently shows examples of binding the return value of the method to a service bus queue, but not how to set up the binding for the ICollector object. But yes, I did mean to add the binding annotation for the ICollector object to this answer if it is necessary to use this pattern. – IronSean Dec 10 '21 at 23:00
1

Also, Functions is built on top of the WebJobs SDK; so if you can do a binding in the SDK, you can do the same thing in Functions (with a few corner case exceptions).

Mike S
  • 3,058
  • 1
  • 22
  • 12
  • Thanks - I have done this where I have had to send messages of to different queues based on the message that has been received. – Harry Miller Aug 08 '17 at 08:32