How to return multiple Service Bus messages from Azure Function?

Viewed 812

Is there a way to output multiple Message (ServiceBusMessage in version 5.0) instances from an Azure Function? The documentation says that one can either return a single message via out Message or multiple entities via ICollector<T>/IAsyncCollector<T>.

I guess the T is a payload type, so using ICollector<Message> would not allow me to configure SessionId and other sending options. Instead, it would simply treat the Message as a payload within an actual message.

2 Answers
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace FunctionApp5
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, 
            [ServiceBus("queuename1", Connection = "ServiceBusConnectionString1")] IAsyncCollector<dynamic> outputServiceBus1,
            [ServiceBus("queuename2", Connection = "ServiceBusConnectionString2")] IAsyncCollector<dynamic> outputServiceBus2,
            ILogger log)
        {
            await outputServiceBus1.AddAsync("xxx");

            await outputServiceBus2.AddAsync("xxx");

            return new OkObjectResult("This is a test.");
        }
    }
}

As @user1672994 pointed out, both ICollector<T> and IAsyncCollector<T> accept ServiceBusMessage as the T. Here is an example from Azure WebJobs Service Bus extension.

Related