Cannot access Amazon SQS message attributes in C#

Ok so I figured this one out. The attribute names need to be specified as a property of the ReceiveMessageRequest object before the call is made to pull the message.

So, the code above needs to change to:

IAmazonSQS sqs = AWSClientFactory.CreateAmazonSQSClient();
ReceiveMessageResponse receiveMessage = new ReceiveMessageResponse();
ReceiveMessageRequest request = new ReceiveMessageRequest();

//Specify attribute list
List<string> AttributesList = new List<string>();
AttributesList.Add("MESSAGEPRIORITY");

//Assign list and QueueURL to request
request.MessageAttributeNames = AttributesList;
request.QueueUrl = "myURL";

//Receive the message...
receiveMessage = sqs.ReceiveMessage(request);
//Body...
string messageBody = receiveMessage.Messages[0].Body;
//...and attributes
Dictionary<string, MessageAttributeValue> messageAttributes = receiveMessage.Messages[0].MessageAttributes;

The above works for me. Hopefully it'll be useful to someone....


To retrieve all attributes of a message without specifying each one you can put "*" or "All" in your attributes list. Like so :

//Specify attribute list
List<string> AttributesList = new List<string>();
AttributesList.Add("*");

AWS SQS ReceiveMessage documentation