Receive a Message from an Amazon SQS Queue - AWS SDK for .NET (version 2)

End of support announcement:

This documentation is for version 2.0 of the AWS SDK for .NET. For current content, see the latest version of the AWS SDK for .NET developer guide instead.

Receive a Message from an Amazon SQS Queue

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

To receive 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:

    ReceiveMessageRequest 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:

    ReceiveMessageResponse receiveMessageResponse = amazonSQSClient.ReceiveMessage(receiveMessageRequest);

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

  3. The response object contains a ReceiveMessageResult member. This member includes a Messages list. Iterate through this list to find a specific message, and use the Body property to determine if the list contains a specified message, as follows:

    if (result.Message.Count != 0) { for (int i = 0; i < result.Message.Count; i++) { if (result.Message[i].Body == messageBody) { receiptHandle = result.Message[i].ReceiptHandle; } } }

    Once the message is found in the list, use the ReceiptHandle property to obtain a receipt handle for the message. You can use this receipt handle to change message visibility timeout or to delete the message from the queue. For more information about how to change the visibility timeout for a message, go to ChangeMessageVisibility.

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

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