Creating a Simple .NET Amazon SQS Message Producer Application

Advertisements

This is a multipart series on Interapplication Messaging with .NET and AWS Simple Queue Service (SQS). Checkout all articles in the series.

Simple Interapplication Messaging with .NET and AWS Simple Queue Service
Create an AWS SQS Queue from the Console
Creating a Simple .NET AWS SQS Message Producer Application
Create a Simple .NET Application to Consume an AWS SQS Queue

In interapplication messaging there are at least two parties that participate and those parties can be classified as producers and consumers. In this tutorial we will focus on the message producer or the party that creates and sends a message to a queue.

The Solution

We will create a simple .NET console application that produces messages and queues them using an Amazon SQS queue.

Remember, for any example solution from AWS with .NET, we focus on the code that exemplifies the problem we are trying to solve. We don’t include logging, input validation, exception handling, etc., and we embed the configuration data within classes instead of using environment variables, configuration files, key/value stores and the like. These items should not be skipped for proper solutions.

Prerequisites

To complete this solution you will need the .NET CLI which is included in the .NET SDK. In addition, you will need to create an AWS IAM user with programmatic access with the appropriate permissions. For this solution you can use the AmazonSQSFullAccess policy. After creating your IAM user, record the access key and secret for later use. If you have not completed the exercise in the first article in this series, you will also need to create an SQS queue and record the queue URL.

Warning: some AWS services may have fees associated with them.

Developing The .NET Amazon SQS Consumer

Let’s create the AWS SQS Producer by entering the following command in the console.

$ dotnet new console ––name SQSProducer

After creating the console application, we need to navigate to the directory of that app.

$ cd SQSProducer

Let’s add a package reference to the latest .NET SQS SDK.

$ dotnet add package AWSSDK.SQS ––version 3.5.1.19

Our application is now created and a reference to the SQS SDK has been added. Let’s create a class for our SQS Producer.

namespace SQSProducer
{
public class SQSMessageProducer{
public SQSMessageProducer()
{
}
}
}

The SQS message producer is a very simple application with only one method, Send. The Send method takes one string parameter named, message. The message parameter is the text that will be the body of the SQS message.

In the Send method we will wire up the configuration for the calls to Amazon SQS. The data for the configuration settings of accessKey, secret, queueUrl, awsregion should be obtained from the posts specified in the prerequisites.

We need to create a SendMessageRequest passing in our message parameter and the queueUrl variable.

In order to send the message to the SQS queue, we will need to instantiate an AmazonSQSClient passing in our credentials and the AWS region. Once we have our AmazonSQSClient instantiated, we can then call the SendMessageAsync method on the AmazonSQSClient, passing in the SendMessageRequest that we previosuly created.

NOTE: The method of storing and using credentials below is used for simplicity. Following one of these other methods to store your AWS credentials with .NET is recommended.

public async Task Send(String message)
{
string accessKey = "<AWS ACCESS KEY>";
string secret = "<AWS ACCESS KEY>";
string queueUrl = "<AWS ACCESS KEY>";
string awsregion = "<AWS ACCESS KEY>";
BasicAWSCredentials creds = new BasicAWSCredentials(accessKey, secret);
RegionEndpoint region = RegionEndpoint.GetBySystemName(awsregion);
SendMessageRequest sendMessageRequest = new SendMessageRequest(queueUrl, message);
var sqsClient = new AmazonSQSClient(creds, region);
await sqsClient.SendMessageAsync(sendMessageRequest);
}

Lastly, we need to instantiate an SQSMessageProducer object, then call the Send method within Program.cs. Each time the SQSProducer application is run, it will create and queue one message.

namespace SQSProducer
{
class Program
{
static async Task Main(string[] args)
{
var message = "New Message for " + DateTime.Now.ToString();
SQSMessageProducer sQSMessageProducer = new SQSMessageProducer();
await sQSMessageProducer.Send(message);
Console.WriteLine("Message Sent: " + message);
}
}
}

That’s it. That’s all there is to creating a simple .NET Amazon SQS message producer application.

Continue on to the next article in this series: Create a Simple .NET Application to Consume an AWS SQS Queue.

Get all the source code at GitHub!

Advertisements
%d bloggers like this: