Build an AWS SQS Background Service with .NET 5 — Part 1
By: Martin Massé
Tags: Cloud, AWS, Csharp, Sqs, Web Development

The scope of this article it to get you started building a SQS background services using .NET 5 (should work with 3.1 as well).

SQS stands for Simple Queue Service. That is, it’s a queue service. What you can do with it is pretty straightforward.

You create a queue You put messages in the queue You read messages from the queue You process the messages On success, you remove the messages from the queue

What happens when something fails? Well, the message stay in the queue for reprocessing until future success. It means that you can use SQS to easily build asynchronous resilient cross platform applications.

Prerequisites

You will need this to run the tutorial code.

AWS Account An IAM user with SQS access rights .NET 5 (or 3.1)

Step 0 — Project setup

Create a new Web project. It can be a worker or Web API or any other project type.

dotnet new webapi -o SQSDemoBackgroundService or dotnet new worker -o SQSDemoBackgroundService

Add the AWSSDK.SQS package

dotnet add package AWSSDK.SQS

Step 1 — Create the background service

Add a new class called SQSBackgroundService inheriting from the BackgroundService class. Implement the Execute method to make sure the code compile. Your new background service class should look like this:

The next step would be to register the background service in Startup.cs

The background service is now ready for the next step. If you start the application, it will crash...so don’t do that.

Step 2 — Creating the queue

You can skip this step if you already know all this and have a queue ready for use.

Option A: Create the Queue using the AWS Console

Login to your AWS account and look for SQS.

Select “Create Queue”.

Enter a name for the queue e.g MyDemoQueue.

You may want to change the settings to better fit your need but it is not required for this tutorial. You may learn more by looking at the AWS SQS documentation.

Select “Create Queue” at the bottom of the page to complete queue creation.

Option B: Create the queue with code

SQS queue can also be created by code using the SDK. The best place to do this is in the background service Execute methods. The idea is to check if the queue already exists. If it does not, a new queue is created using the given name.

The background service now look like this:

Start the application to check if everything run fine.

Go back to the SQS home page, you should see something like this:

Since this is a brand new queue, you can see there are zero messages available and zero messages in flight. An “available” message is a message waiting for you to read from the queue. An “in flight” message is a message read for the queue, currently being processed.

Step 3 — Reading from the queue and process messages

The next step would be to read the message from the queue. At this time, there are no messages available, we will get back to this in the next step. First we need to have a procedure that read the queue, process the messages and remove the messages processed with success.

For this tutorial, processing the item means outputting the message to the console.

Step 4 — Put a message on the queue

There are many ways to put messages in the queue. The easiest way to test this is to post a new message to the queue using the AWS Console.

Follow those steps to send a message using the Console

Open the SQS service page in the AWS Console Click in the queue Select “Send and receive messages” Enter the message you want to send in the “Send message” section (I have entered “Message test sent from the AWS console”) Select “Send message”

Look at the dotnet output window, you should see something similar

This can also be done by code. The code can be anywhere…a controller, a service, another platform. Name it. The only limit is your imagination.

To send a message you use the SendMessageAsync method from the IAmazonSQS client.

I have updated the test code to send a dummy message when the background service start.

Step 5 — Complete code

The code for this article is available on GitHub at

https://github.com/martin-masse/SQSDemoBackgroundService