Introduction
Amazon Simple Queue Service (SQS) can be integrated in your .net application via https get/post calls or via SDK. This article explains the step by step process for interacting with SQS using SDK/.net libraries.

Note: This article is intended to get you started with Amazon Simple Queue Service but doesn’t necessarily follow the best security practices. Please consider the Best Practices for Managing AWS Access Keys for your actual implementation.

Download the AWS SDK
Download AWS SDK for .NET MSI installer from here and install it. You can also install from nugget, but this article references assembly paths installed by MSI installer.

Add Assembly References
In order to Interact with AWS Services, you will need to add reference to the following assemblies into your application from SDK install directory. If you installed the SDK on default path, these assemblies can be found in C:\Program Files (x86)\AWS SDK for .NET\bin\Net45\

  • AWSSDK.Core.dll
  • AWSSDK.SQS.dll

Create SQS Queues
Follow the steps below for creating SQS queue.

  1. Sign in to the AWS Management Console and open the Amazon SQS console at https://console.aws.amazon.com/sqs/. Click Create New Queue.

image001

  1. In the Create New Queuedialog box, enter MyQueue in the Queue Name field, and leave the default value settings for the remaining fields.

image002

  1. Click Create Queue.
  2. Your new queue appears in the list of queues.

image003

  1. Please note down the queue URL for later use in below steps.

Create SQS Client Object

In order to interact with the queue, you will need to create and initialize queue object

  1. Create a new class file and Import the following namespaces into your class as shown below

using Amazon;
using Amazon.SQS;
using Amazon.SQS.Model;

  1. In order create a SQS client object you will need AWS credentials i.e. AWS Access Key and AWS Secret Access Key. Please see steps for Getting Access Key ID and Access Secret Key to get the AWS access Key ID and Secrete Access key. Please use the access keys for the same user that you use to create the queue or add the user to a group that has AmazonSQSFullAccess policy attached.
  1. Create and initialize Amazon SQS client as shown below

string awsAccessKeyId = "AKIAQ5GG7B3G7JB247MQ";
string awsSecretAccessKey = "ETtXd1s5yjbMihc1njMxe-fd3w2pRCrI04v2J+sa";
IAmazonSQS sqs = new AmazonSQSClient(awsAccessKeyId,awsSecretAccessKey , RegionEndpoint.USWest2);

Sending Message to Queue

Following code creates a message and sends it to “MyQueue” created above.

//Sending a message
//URL from queue creation steps
String myQueueUrl = "https://sqs.us-west-2.amazonaws.com/654472398987/MyQueue"
Console.WriteLine("Sending a message to MyQueue.\n");
SendMessageRequest sendMessageRequest = new SendMessageRequest();
sendMessageRequest.QueueUrl = myQueueUrl;
sendMessageRequest.MessageBody = "This is my message text.";sqs.SendMessage(sendMessageRequest);

Receiving Messages from Queue
Following code retrieves the message from the queue and writes it’s properties the console.

//Receiving a message
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest();
receiveMessageRequest.QueueUrl = myQueueUrl;
ReceiveMessageResponse receiveMessageResponse = sqs.ReceiveMessage(receiveMessageRequest);
Console.WriteLine("Printing received message.\n");
foreach (Message message in receiveMessageResponse.Messages)
{
Console.WriteLine("  Message");
Console.WriteLine("    MessageId: {0}", message.MessageId);
Console.WriteLine("    ReceiptHandle: {0}", message.ReceiptHandle);
Console.WriteLine("    MD5OfBody: {0}", message.MD5OfBody);
Console.WriteLine("    Body: {0}", message.Body);
foreach (KeyValuePair<string, string> entry in message.Attributes)
{
Console.WriteLine("  Attribute");
Console.WriteLine("    Name: {0}", entry.Key);
Console.WriteLine("    Value: {0}", entry.Value);
}
}
//save the message handle for next steps
String messageRecieptHandle = receiveMessageResponse.Messages[0].ReceiptHandle;

Please note that message will not be deleted from the queue but will be invisible to other clients for time specified in “Default Visibility Timeout” property of the queue shown in queue creation screenshot. Next section shows how to delete the message from the queue.

Delete Message from Queue

Following code uses the message receipt handle of the received message to specify the message to be deleted.

//Deleting a message
Console.WriteLine("Deleting the message.\n");
DeleteMessageRequest deleteRequest = new DeleteMessageRequest();
deleteRequest.QueueUrl = myQueueUrl;
deleteRequest.ReceiptHandle = messageRecieptHandle;
sqs.DeleteMessage(deleteRequest);

Summary

Integrating Amazon Simple Queue Service – Amazon SQS into your application using .NET SDK is easy. Following are high level steps to accomplish the task.

  • Download and install SDK
  • Add assembly references
  • Create queue and credentials to connect
  • Write code to work with the queue