Is that possible to use multiple output in Azure Functions .Net 5?

Viewed 724

I'm using .net 5 azure function with ServiceBus. I want to send multiple messages from trigger function. In previous version you could usr IAsyncCollector to do something like that:

[FunctionName("HttpToServiceBusQueue")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
    [ServiceBus("testqueue",Connection ="connectionString")] IAsyncCollector<string> outputEvents,
    ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");
    // ...
    
    await outputEvents.AddAsync("message1");
    await outputEvents.AddAsync("message2");

    // ...
    return new OkObjectResult(responseMessage);
}

But as I read there in documentation is no support of IAsyncCollector in newer version. Is there any alternative ways to do it or?

Example of my code:

[Function("FileTriggerFunction")]
[ServiceBusOutput("fileupload", Connection = "ServiceBusConnectionWrite")]
public string Run(
    [BlobTrigger("file-storage/{name}", Connection = "ConnectionString")] string  myBlob, string name,
    FunctionContext context)
{
    var logger = context.GetLogger("FileTriggerFunction");
    var res = JsonConvert.SerializeObject(*List of messages*);
    logger.LogInformation(res);
    return res;
}

Actually it separates each object of list for separate message, but I don't think that it is correct to do this in such way.

2 Answers

Instead of IAsyncCollector we have multiple output binding in .net 5

On this function we will give the multiple output values.

public static class MultiOutput
{
[Function("MultiOutput")]
public static MyOutputType Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req,
FunctionContext context)
{
    var response = req.CreateResponse(HttpStatusCode.OK);
    response.WriteString("Success!"); 
    string myQueueOutput = "some output";
    return new MyOutputType()
    {
        Name = myQueueOutput,
        HttpResponse = response
    };
    }
}
public class MyOutputType
{
    [QueueOutput("myQueue")]
    public string Name { get; set; }
    public HttpResponseData HttpResponse { get; set; }
}

Refer .NET isolated process guide for .NET 5.0 in Azure Functions

Thanks @ Stephen Cleary

I had tried with SeviceBus Client to send a multiple messages to a queue I am able to process it.

Refer here

Here is a sample code how to do it

    [Function("MultiOutput")]
    public static DispatchedMessages Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req,
        FunctionContext context)
    {
        return new DispatchedMessages
        {
            Messages = new List<string> { "aaa", "bbb" } // todo: serialize
        };
    }

    public class DispatchedMessages
    {
        [ServiceBusOutput(queueOrTopicName: "dest", Connection = "AzureServiceBus")]
        public IEnumerable<string> Messages { get; set; }
    }
}
Related