无法在C#中访问Amazon SQS消息属性 [英] Cannot access Amazon SQS message attributes in C#

查看:142
本文介绍了无法在C#中访问Amazon SQS消息属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建SQS消息并将其放置在SQS队列中的过程,还有一个读取这些消息并根据消息主体的内容和属性执行某些逻辑的过程.

I have a process that creates SQS messages and places them on an SQS queue and another process that reads those messages and performs certain logic based on the contents of the body and attributes of the message.

我可以在带有正文和属性的SQS队列上成功创建一条消息,但是回读消息属性时遇到问题!

I can successfully create a message on the SQS queue with body and attributes, but I have a problem when reading the message attributes back!

我确定消息创建过程正确无误,可以在AWS SQS控制台中看到队列的属性.我只是无法弄清楚为什么我不能读回那些属性.

I am sure my message creation process is correct, I can see the attributes in the AWS SQS console for the queue. I just cannot figure out why I cannot read those attributes back.

我创建消息的代码:

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控制台中看到该消息和属性).

The above works and a message with attribute MESSAGEPRIORITY=HIGH is created (I can see the message and attribute in the SQS Console).

回读消息时(我已经跳过了显示队列URL"等的代码部分):

When reading back the message (I've skipped parts of the code that show Queue URL etc):

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队列手动创建消息时,我遇到相同的问题. 我不知道为什么吗? 任何帮助将是巨大的. 非常感谢,

With the above code, I get the body of the message, but the attributes are empty! I have the same problem when creating the message manually using the SQS queue directly. I can't figure out why? Any help would be great. Many thanks,

推荐答案

好,所以我想到了这一点. 在进行调用以提取消息之前,需要将属性名称指定为ReceiveMessageRequest对象的属性.

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....

这篇关于无法在C#中访问Amazon SQS消息属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆