3

I am trying to get the message Id of the currently processing message in a Queue in Azure web job. Couldn't find any proper documentation on how to get that.

public static void ProcessQueueMessage([QueueTrigger("%testingQueue%")] TestingMessageModel testMessage, TextWriter log)
{
   // want to do some logging for this particular triggered message using the messageid. How to get that?
}

Adding the TestingMessageModel as a reference, it doesn't have any guid. I want to use the GUID that azure creates when a message is put into the queue.

public class TestingMessageModel
{
  public int FromOrg {get; set;}
  public DateTime BatchDate {get; set;}
  public Payments[] payments {get; set;}
}
Anurag
  • 78
  • 9

2 Answers2

8

It supports to bind the id directly, you could check my code.

public static void ProcessQueueMessage([QueueTrigger("myqueue")] string message,ILogger logger, string id)
        {
            logger.LogInformation(message);
            logger.LogInformation($"{message}id={id}");
        }

enter image description here

enter image description here

Hope this could help you.

Community
  • 1
  • 1
George Chen
  • 13,703
  • 2
  • 11
  • 26
  • Thanks George. That really helped. It's weird that microsoft doesn't put this into the documentation and we have to try it out. – Anurag Jul 16 '19 at 15:27
  • 1
    @Anurag Came across this post and am trying to do the same thing. But my id is always null, and my constructor looks just like the one you have above. Is that really all you need to do is add the "string id" parameter? Or is there something else? – Mike Feb 11 '21 at 00:22
  • This doesn't work for me either. I've also tried using CloudQueueMessage and it complains that the message isn't JSON, as it's trying to map the message into the CloudQueueMessage model. – Rob Bramhall Aug 09 '22 at 18:05
3

The queue trigger provides several metadata properties (including the message id).

These properties can be used as part of binding expressions in other bindings or as parameters in your code:

enter image description here

See https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-queue-trigger?tabs=csharp#message-metadata

Maicon Heck
  • 2,017
  • 1
  • 20
  • 27