1

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...

Jerry Liu
  • 17,282
  • 4
  • 40
  • 61
Josh
  • 6,944
  • 8
  • 41
  • 64
  • I think you need to use Message with a stream https://stackoverflow.com/questions/49045149/difference-between-brokeredmessage-class-in-microsoft-servicebus-and-message-cla – Carlos Alves Jorge Jan 04 '19 at 12:43

1 Answers1

5

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);
    }
Jerry Liu
  • 17,282
  • 4
  • 40
  • 61