AWS SDK Version 2 for .NET
API Reference

AWS services or capabilities described in AWS Documentation may vary by region/location. Click Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

.NET Framework 4.5
 
Retrieves one or more messages, with a maximum limit of 10 messages, from the specified queue. Long poll support is enabled by using the
WaitTimeSeconds
parameter. For more information, see Amazon SQS Long Poll in the Amazon SQS Developer Guide.

Short poll is the default behavior where a weighted random set of machines is sampled on a

ReceiveMessage
call. This means only the messages on the sampled machines are returned. If the number of messages in the queue is small (less than 1000), it is likely you will get fewer messages than you requested per
ReceiveMessage
call. If the number of messages in the queue is extremely small, you might not receive any messages in a particular
ReceiveMessage
response; in which case you should repeat the request.

For each message returned, the response includes the following:

The receipt handle is the identifier you must provide when deleting the message. For more information, see Queue and Message Identifiers in the Amazon SQS Developer Guide.

You can provide the

VisibilityTimeout
parameter in your request, which will be applied to the messages that Amazon SQS returns in the response. If you do not include the parameter, the overall visibility timeout for the queue is used for the returned messages. For more information, see Visibility Timeout in the Amazon SQS Developer Guide.

Going forward, new attributes might be added. If you are writing code that calls this action, we recommend that you structure your code so that it can handle new attributes gracefully.

Namespace: Amazon.SQS
Assembly: AWSSDK.dll
Version: 2.3.14.0

Syntax

C#
public virtual ReceiveMessageResponse ReceiveMessage(
         ReceiveMessageRequest request
)

Parameters

request
Type: Amazon.SQS.Model.ReceiveMessageRequest

Container for the necessary parameters to execute the ReceiveMessage service method.

Return Value
Type: Amazon.SQS.Model.ReceiveMessageResponse
The response from the ReceiveMessage service method, as returned by SQS.

Exceptions

Exception Condition
OverLimitException The action that you requested would violate a limit. For example, ReceiveMessage returns this error if the maximum number of messages inflight has already been reached. AddPermission returns this error if the maximum number of permissions for the queue has already been reached.

Examples

This example shows how to receive a message.

Receive message example

var client = new AmazonSQSClient();

var request = new ReceiveMessageRequest
{
  AttributeNames = new List<string>() { "All" },
  MaxNumberOfMessages = 5,
  QueueUrl = "https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyTestQueue",
  VisibilityTimeout = (int)TimeSpan.FromMinutes(10).TotalSeconds,
  WaitTimeSeconds = (int)TimeSpan.FromSeconds(5).TotalSeconds
};

var response = client.ReceiveMessage(request);

if (response.Messages.Count > 0)
{
  foreach (var message in response.Messages)
  {
    Console.WriteLine("For message ID '" + message.MessageId + "':");
    Console.WriteLine("  Body: " + message.Body);
    Console.WriteLine("  Receipt handle: " + message.ReceiptHandle);
    Console.WriteLine("  MD5 of body: " + message.MD5OfBody);
    Console.WriteLine("  MD5 of message attributes: " +
      message.MD5OfMessageAttributes);
    Console.WriteLine("  Attributes:");

    foreach (var attr in message.Attributes)
    {
      Console.WriteLine("    " + attr.Key + ": " + attr.Value);
    }
  }
}
else
{
  Console.WriteLine("No messages received.");
}
      

Version Information

.NET Framework:
Supported in: 4.5, 4.0, 3.5