如何 return 来自 Azure Function 的多条服务总线消息?

How to return multiple Service Bus messages from Azure Function?

有没有办法从 Azure 函数输出多个 MessageServiceBusMessage 版本 5.0)实例?文档 says 可以通过 out Message return 单个消息或通过 ICollector<T>/IAsyncCollector<T>.

多个实体

我猜 T 是一种负载类型,所以使用 ICollector<Message> 不允许我配置 SessionId 和其他发送选项。相反,它会简单地将 Message 视为实际消息中的有效载荷。

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.");
        }
    }
}

正如@user1672994 所指出的,ICollector<T>IAsyncCollector<T> 都接受 ServiceBusMessage 作为 T。这是来自 Azure WebJobs 服务总线扩展的 an example