Amazon Simple Queue Service Using C#

SQS (Amazon Simple Queue Services)

 
Amazon SQS (Amazon Simple Queue Services) is a queuing service used for high-throughput, system-to-system messaging. You can use queues to decouple heavyweight processes and to buffer and batch work. Amazon SQS stores messages until microservices and serverless applications process them.
 

Steps to implementing Amazon SQS

 
Create a Queue
 
You will have the provide the name of the Queue to create a simple queue in the Amazon cloud.
 
Create QueueURL
 
After this, you have to create a queue URL that would basically build with your configuration you can create this on your own by providing your location, your queue name, etc.
 
Example - https://{region_name}/queue.|apidomain|/{account_number}/{queue_name}
 
Note
region_name is one of the most important parts of this queue url else this would process the complete flow but you will not be able to find the queue on amazon console, because of the region mismatch.
 
Process queue
 
You need to create a message request with the queue URL and the message body and send it to the available queue.
 
Receive Message
 
The last step is to receive the messages sent to the defined queue. You need to create a receive request and call the receive request method of the SQS client.
 
You can follow the same process for using this simple queue service over the Amazon Console too. You need to login to the console first. After that, you will see a screen like this.
 
Type "Simple Queue Service" in the textbox and select from the results shown on the screen.
 
 
Here, you will have the list of a queue available on your own AWS the above screenshot, I have created two queues named:
  1. TestingQueue
  2. TestingQueue1
 
I am going to show you a working example of the ASQS in .NET using Visual Studio.
 
Let's begin.
 
Step 1
 
 
 
Step 2
 
Create a Windows or web application or whatever you want. I have created a Windows.Form application and have taken two buttons one for creating the queues and other for processing the queue.
 
 
Step 3
 
After this, you need to set up your project to use the Amazon Services. First, you have to specify the Access Key, Secret Key, and Region in web.config if you are using the web application; and in app.config if you are using the Windows application.
  1. <appSettings>  
  2.   <add key="AWSAccessKey" value="*******************" />  
  3.   <add key="AWSSecretKey" value="***********************************" />  
  4.   <add key="AWSRegion" value="*********" />  
  5. </appSettings>  
Remember, the key name must be in the case as mentioned. and make sure you enter the correct region from aws console.
 
Step 4
 
After this, you need to install a few of the necessary .dll files from the NuGet Package Manager. You can add these required libraries from the NuGet Package Manager or PM> console by typing the name of the libraries, as shown in the references.
 
Now, it's time to write a few lines of code for the same.
  1. using Amazon.SQS;  
  2. using Amazon.SQS.Model;  
  3. using System;  
  4. using System.Linq;  
  5. using System.Windows.Forms;  
  6.   
  7. namespace AmazonSQS  
  8. {  
  9.     public partial class Form1 : Form  
  10.     {  
  11.         public AmazonSQSConfig SqsConfig { getset; }  
  12.         public AmazonSQSClient SqsClient { getset; }  
  13.         public string QueueUrl { getset; }  
  14.         public string  MessageBody { get { return "This is a simple message queue test"; }}  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.             SqsConfig = new AmazonSQSConfig  
  19.             {  
  20.                 ServiceURL = "http://sqs.us-east-2.amazonaws.com"  
  21.             };  
  22.             SqsClient = new AmazonSQSClient(SqsConfig);  
  23.             QueueUrl = "https://sqs.*********.amazonaws.com/*************/TestingQueue1";  
  24.         }  
  25.   
  26.         private void BtnCreateQueue_Click(object sender, EventArgs e)  
  27.         {  
  28.             var createQueueRequest = new CreateQueueRequest();  
  29.             createQueueRequest.QueueName = "TestingQueue123";  
  30.   
  31.             var createQueueResponse = SqsClient.CreateQueue(createQueueRequest);  
  32.             MessageBox.Show(createQueueRequest.QueueName + " queue created successfully..");  
  33.         }  
  34.   
  35.         private void BtnProcessQueue_Click(object sender, EventArgs e)  
  36.         {  
  37.             // sending the message to the message queue  
  38.             var sendMessageRequest = new SendMessageRequest  
  39.             {  
  40.                 QueueUrl = QueueUrl,  
  41.                 MessageBody = "This is a simple message queue test"  
  42.             };  
  43.             var sendMessageResponse = SqsClient.SendMessage(sendMessageRequest);  
  44.   
  45.             //receiving the message from message queue  
  46.             var receiveMessageRequest = new ReceiveMessageRequest  
  47.             {  
  48.                 QueueUrl = QueueUrl  
  49.             };  
  50.             var receiveMessageResponse = SqsClient.ReceiveMessage(receiveMessageRequest);  
  51.             var messages = receiveMessageResponse.Messages;  
  52.   
  53.             if (messages.Any())  
  54.             {  
  55.                 for (int i = 0; i < messages.Count(); i++)  
  56.                 {  
  57.                     if (messages[i].Body == MessageBody)  
  58.                     {  
  59.                         var receiptHandle = messages[i].ReceiptHandle;  
  60.                         MessageBox.Show(messages[i].Body + "found in Queue");  
  61.                     }  
  62.                 }  
  63.             }  
  64.             else {  
  65.                 MessageBox.Show("failed to process...");  
  66.             }  
  67.         }  
  68.     }  
  69. }  
If you successfully processed the queue you will get the message.body + “found in Queue” message at message box, 
 
If you have any queries or questions, I will be happy to answer.
 
For the implementation of amazon notification services, you can referer: 
Amazon Simple Notification Services using C#
 
Happy coding! Enjoy.


Similar Articles