Search code examples
.netazuresdkazure-storage-queues

Get messageId of message sent to Azure queue with .NET v12 SDK


How can I get the messageId of a message I send to a queue using .NET v12?

This answer https://stackoverflow.com/a/56407472/11636360 in Get message ID in Azure queue shows how it can be done in .NET v11, but .NET v12 SendMessage method only accepts a string input.

Here's a code snippet from Microsoft website to put a message in a queue using .NET v12.

Cycling through PeekMessages afterwards and look for a message with the same content is all I can think of but doesn't seem very neat or would necessarily work with a large number of messages in the queue.

// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];

// Instantiate a QueueClient which will be used to create and manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, queueName);

// Create the queue if it doesn't already exist
queueClient.CreateIfNotExists();

if (queueClient.Exists())
{
    // Send a message to the queue
    queueClient.SendMessage(message);
}

Solution

  • SendMessage method returns an object of type Response<SendReceipt>. Looking at the documentation for SendReceipt, you should be able to get the message id from its MessageId property.

    Please try something like:

    if (queueClient.Exists())
    {
        // Send a message to the queue
        var response = queueClient.SendMessage(message);
        var messageId = response.Value.MessageId;
    }