AWS SDK for .NET
Developer Guide

Receiving a Message from an Amazon SQS Queue

You can use the AWS SDK for .NET to receive messages from an Amazon SQS queue.

To receive a message from an Amazon SQS queue

For information about sending a message to your queue, see Sending an Amazon SQS Message.

For more information about deleting a message from the queue, see Deleting a Message from an Amazon SQS Queue.

  1. Create and initialize a ReceiveMessageRequest instance. Specify the queue URL to receive a message from, as follows.

    var receiveMessageRequest = new ReceiveMessageRequest(); receiveMessageRequest.QueueUrl = myQueueURL;

    For more information about your queue URL, see Your Amazon SQS Queue URL.

  2. Pass the request object as a parameter to the ReceiveMessage method, as follows.

    var receiveMessageResponse = sqsClient.ReceiveMessage(receiveMessageRequest);

    The method returns a ReceiveMessageResponse instance, containing the list of messages the queue contains.

  3. The ReceiveMessageResponse.ReceiveMessageResult property contains a ReceiveMessageResponse object, which contains a list of the messages that were received. Iterate through this list and call the ProcessMessage method to process each message.

    foreach (var message in result.Messages) { ProcessMessage(message); // Go to a method to process messages. }

    The ProcessMessage method can use the ReceiptHandle property to obtain a receipt handle for the message. You can use this receipt handle to change the message visibility timeout or to delete the message from the queue. For more information about how to change the visibility timeout for a message, see ChangeMessageVisibility.