简体   繁体   中英

Is it possible to output Message/BrokeredMessage with Azure Functions V2?

It isn't clear from the docs how to output a structured message. In an old function I've used BrokeredMessage , and the docs say to use Message for V2 functions, however there is no guidance on how to use this. Is this correct:

[FunctionName(nameof(Job))]
public static async Task<IActionResult> Job(
    // ...
    IAsyncCollector<Microsoft.Azure.ServiceBus.Message> serializedJobCollector
)

The goal is to be able to set some metadata properties like the ID, which I've done before (with V1 and BrokeredMessage ) for duplicate detection, but I'm not sure if this is correct or I need to serialize to a string or what...

You have found the right way, as the doc says

for 2.x, use Message instead of BrokeredMessage

To take an example

    [FunctionName("FunctionTest")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
        [ServiceBus(queueOrTopicName:"queueName",Connection ="ServiceBusConnection")]IAsyncCollector<Message> outputMessages,
        ILogger log)
    {
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            var message = new Message
            {
                Body = System.Text.Encoding.UTF8.GetBytes(requestBody),
                MessageId = "MyMessageId"
            };
            await outputMessages.AddAsync(message);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM