从Amazon SQS获取消息属性名失败问题求助
Fixing the Issue of Missing Message Attributes in Your Amazon SQS Listener
Hey there, I spot the problem in your code right away—you're mixing up queue attributes and message attributes! Let's break this down and get those message attributes showing up for your listener.
What's Going Wrong
Your current code adds "ContactsQueue" to an AttributesList and uses that with ReceiveMessageRequest, but that list is for queue-level attributes (like the queue's visibility timeout or creation date), not the custom message attributes you attached when sending form data to SQS.
To pull in the message-specific attributes you can see in the SQS console, you need to target the MessageAttributeNames property instead of AttributeNames.
Corrected Code Example
// Set up your ReceiveMessageRequest as before ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(); receiveMessageRequest.QueueUrl = urlSQS; receiveMessageRequest.MaxNumberOfMessages = 10; // This is the list for message attributes, not queue attributes List<string> messageAttributeNames = new List<string>(); // Use "All" to pull every message attribute, or list specific ones like "UserEmail" or "SubmissionTime" messageAttributeNames.Add("All"); // Assign this list to the correct property on the request receiveMessageRequest.MessageAttributeNames = messageAttributeNames; // Fetch the messages from SQS var receiveResponse = sqsClient.ReceiveMessage(receiveMessageRequest); // Now you can access both the user data body and message attributes foreach (var message in receiveResponse.Messages) { // Get your user form data from the message body string userData = message.Body; // Access individual message attributes if (message.MessageAttributes.TryGetValue("YourCustomAttributeName", out var attribute)) { string attributeValue = attribute.StringValue; // Handle this value (e.g., save it to your MS-SQL database) } }
Key Notes
- If you only need specific attributes (not all), replace
"All"with the exact names of the attributes you set when sending the message to SQS. - Since you mentioned the attributes are visible in the SQS console, you already have them correctly attached to messages—you just weren't asking the listener to retrieve them.
- Always keep this distinction clear:
AttributeNamesrefers to queue-level properties, whileMessageAttributeNamesrefers to properties attached to individual messages.
内容的提问来源于stack exchange,提问作者Shubham Khandelwal
相关产品推荐
相关产品推荐

