无法在C#中访问Amazon SQS消息属性

时间:2014-05-09 12:28:40

标签: c# amazon-web-services amazon-sqs

我有一个创建SQS消息并将它们放在SQS队列上的进程,以及另一个读取这些消息并根据正文内容和消息属性执行某些逻辑的进程。

我可以使用正文和属性在SQS队列上成功创建一条消息,但是在阅读消息属性时我遇到了问题!

我确信我的消息创建过程是正确的,我可以在AWS SQS控制台中看到队列的属性。我只是想不通为什么我不能读回那些属性。

我创建邮件的代码:

IAmazonSQS sqs = AWSClientFactory.CreateAmazonSQSClient();
var sendMessageRequest = new SendMessageRequest();
sendMessageRequest.QueueUrl = "myURL";
Dictionary<string, MessageAttributeValue> MessageAttributes = new Dictionary<string, MessageAttributeValue>();
MessageAttributeValue messageTypeAttribute = new MessageAttributeValue();
messageTypeAttribute.DataType = "String";
messageTypeAttribute.StringValue = "HIGH";
MessageAttributes.Add("MESSAGEPRIORITY", messageTypeAttribute);

sendMessageRequest.MessageAttributes = MessageAttributes;
sendMessageRequest.MessageBody = "Thats the message body";

sqs.SendMessage(sendMessageRequest);

上面的工作和创建了属性为MESSAGEPRIORITY = HIGH的消息(我可以在SQS控制台中看到消息和属性)。

回读邮件时(我已跳过显示队列网址等的部分代码):

IAmazonSQS sqs = AWSClientFactory.CreateAmazonSQSClient();
ReceiveMessageResponse receiveMessage = new ReceiveMessageResponse();
ReceiveMessageRequest request = new ReceiveMessageRequest();
request.QueueUrl = "myURL";
receiveMessage = sqs.ReceiveMessage(request);
string messageBody = receiveMessage.Messages[0].Body;
Dictionary<string, MessageAttributeValue> messageAttributes = receiveMessage.Messages[0].MessageAttributes;

使用上面的代码,我得到了消息的正文,但属性是空的! 我直接使用SQS队列手动创建消息时遇到同样的问题。 我想不通为什么? 任何帮助都会很棒。 非常感谢,

3 个答案:

答案 0 :(得分:18)

好的,所以我想出了这个。 在调用拉取消息之前,需要将属性名称指定为ReceiveMessageRequest对象的属性。

因此,上面的代码需要更改为:

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;

以上对我有用。希望它对某人有用......

答案 1 :(得分:8)

要检索邮件的所有属性而不指定每个属性,可以在属性列表中放置“*”或“All”。像这样:

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

AWS SQS ReceiveMessage documentation

答案 2 :(得分:0)

从SQS读取的完整方法

    public async Task ReadFromSQS()
    {
        IAmazonSQS sqs = new AmazonSQSClient(RegionEndpoint.EUWest1);
        try
        {
            List<string> AttributesList = new List<string>();
            AttributesList.Add("NameOfTheAttribute");

            ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest();
            receiveMessageRequest.QueueUrl = IdQueue;
            receiveMessageRequest.MessageAttributeNames = AttributesList;
            ReceiveMessageResponse receiveMessageResponse = await sqs.ReceiveMessageAsync(receiveMessageRequest);

            foreach (Message message in receiveMessageResponse.Messages)
            {
                Debug.WriteLine("Body: "+ message.Body);
                Debug.WriteLine("Values: " + message.MessageAttributes.Count);
                foreach (var entry in message.MessageAttributes)
                {
                    Debug.WriteLine("Attribute");
                    Debug.WriteLine("Name: "+ entry.Key);
                    Debug.WriteLine("Value1: "+ entry.Value.StringValue);
                }
            }
            String messageRecieptHandle = receiveMessageResponse.Messages[0].ReceiptHandle;
            //Deleting a message
            Debug.WriteLine("Deleting the message.\n");
            DeleteMessageRequest deleteRequest = new DeleteMessageRequest();
            deleteRequest.QueueUrl = IdQueue;
            deleteRequest.ReceiptHandle = messageRecieptHandle;
            await sqs.DeleteMessageAsync(deleteRequest);
        }
        catch (AmazonSQSException ex)
        {
            Debug.WriteLine("Caught Exception: " + ex.Message);
            Debug.WriteLine("Response Status Code: " + ex.StatusCode);
            Debug.WriteLine("Error Code: " + ex.ErrorCode);
            Debug.WriteLine("Error Type: " + ex.ErrorType);
            Debug.WriteLine("Request ID: " + ex.RequestId);
        }

        Debug.WriteLine("Press Enter to continue...");
        Console.Read();
    }