MVFC.Messaging.AWS 3.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package MVFC.Messaging.AWS --version 3.0.1
                    
NuGet\Install-Package MVFC.Messaging.AWS -Version 3.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MVFC.Messaging.AWS" Version="3.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MVFC.Messaging.AWS" Version="3.0.1" />
                    
Directory.Packages.props
<PackageReference Include="MVFC.Messaging.AWS" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MVFC.Messaging.AWS --version 3.0.1
                    
#r "nuget: MVFC.Messaging.AWS, 3.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MVFC.Messaging.AWS@3.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MVFC.Messaging.AWS&version=3.0.1
                    
Install as a Cake Addin
#tool nuget:?package=MVFC.Messaging.AWS&version=3.0.1
                    
Install as a Cake Tool

MVFC.Messaging.AWS

🇧🇷 Leia em Português

CI codecov License Platform NuGet Version NuGet Downloads

A .NET messaging provider for Amazon Simple Queue Service (SQS), built on top of MVFC.Messaging.Core. Provides SqsPublisher<T> and SqsConsumer<T> for publishing and consuming JSON-serialized messages in SQS queues with a clean, async-first API.

Package

Package Downloads
MVFC.Messaging.AWS Downloads

Installation

dotnet add package MVFC.Messaging.AWS

This package depends on MVFC.Messaging.Core (installed automatically) and AWSSDK.SQS.

Configuration

AWS Credentials

The SqsPublisher<T> and SqsConsumer<T> both receive an IAmazonSQS client instance, so you control how credentials are provided. Common approaches:

Method Description
Environment variables Set AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and optionally AWS_SESSION_TOKEN
Shared credentials file ~/.aws/credentials with a [default] profile
IAM Role When running on EC2, ECS, or Lambda, the SDK automatically uses the instance/task role
LocalStack For local development, point ServiceURL to http://localhost:4566

Creating the SQS Client

using Amazon.SQS;

// Production — uses the default credential chain (env vars, IAM role, etc.)
var sqsClient = new AmazonSQSClient();

// Local development with LocalStack
var sqsClient = new AmazonSQSClient(new AmazonSQSConfig
{
    ServiceURL = "http://localhost:4566"
});

Queue URL

Both SqsPublisher<T> and SqsConsumer<T> require the queue URL (not the queue name). You can obtain it by creating a queue or calling GetQueueUrlAsync:

var createResponse = await sqsClient.CreateQueueAsync("my-queue");
var queueUrl = createResponse.QueueUrl;
// e.g. "https://sqs.us-east-1.amazonaws.com/123456789012/my-queue"

Usage

Publishing a Single Message

using Amazon.SQS;
using MVFC.Messaging.AWS.SQS;

var sqsClient = new AmazonSQSClient();
var queueUrl = "https://sqs.us-east-1.amazonaws.com/123456789012/my-queue";

await using var publisher = new SqsPublisher<OrderCreated>(sqsClient, queueUrl);

var order = new OrderCreated(1, "Keyboard", 149.90m);
await publisher.PublishAsync(order);

The message is serialized to JSON using System.Text.Json and sent as the SQS message body.

Publishing a Batch

SQS supports sending up to 10 messages in a single SendMessageBatch API call. The PublishBatchAsync method uses this native batch API for optimal performance:

var orders = new[]
{
    new OrderCreated(1, "Keyboard", 149.90m),
    new OrderCreated(2, "Mouse", 59.90m),
    new OrderCreated(3, "Monitor", 899.00m)
};

await publisher.PublishBatchAsync(orders);

Consuming Messages

The consumer runs a background polling loop that calls ReceiveMessage with long polling (5-second wait), processes each message through your handler, and automatically deletes the message from the queue after successful processing:

using Amazon.SQS;
using MVFC.Messaging.AWS.SQS;

var sqsClient = new AmazonSQSClient();
var queueUrl = "https://sqs.us-east-1.amazonaws.com/123456789012/my-queue";

await using var consumer = new SqsConsumer<OrderCreated>(sqsClient, queueUrl);

await consumer.StartAsync(async (message, ct) =>
{
    Console.WriteLine($"Processing order #{message.OrderId}: {message.Product}");
    // Your business logic here
}, cancellationToken);

// ... later, when shutting down:
await consumer.StopAsync();

Consumer behavior:

  • Polls for up to 10 messages per request (SQS maximum).
  • Uses long polling with a 5-second wait time to reduce empty responses.
  • After successful handler execution, the message is automatically deleted from the queue.
  • If the handler throws an exception, the message is not deleted and becomes visible again after the queue's visibility timeout.
  • The polling loop runs until StopAsync is called or the CancellationToken is cancelled.

Complete Publish + Consume Example

using Amazon.SQS;
using MVFC.Messaging.AWS.SQS;

// Setup
var sqsClient = new AmazonSQSClient(new AmazonSQSConfig { ServiceURL = "http://localhost:4566" });
var createResponse = await sqsClient.CreateQueueAsync($"orders-{Guid.NewGuid()}");
var queueUrl = createResponse.QueueUrl;

await using var publisher = new SqsPublisher<OrderCreated>(sqsClient, queueUrl);
await using var consumer = new SqsConsumer<OrderCreated>(sqsClient, queueUrl);

// Start consuming
var received = new TaskCompletionSource<OrderCreated>();
await consumer.StartAsync(async (msg, ct) =>
{
    Console.WriteLine($"Received: Order #{msg.OrderId} — {msg.Product}");
    received.SetResult(msg);
}, CancellationToken.None);

// Publish
await publisher.PublishAsync(new OrderCreated(42, "Keyboard", 149.90m));

// Wait for the message to be consumed
var result = await received.Task.WaitAsync(TimeSpan.FromSeconds(15));

// Cleanup
await consumer.StopAsync();

API Reference

SqsPublisher<T>

Constructor Parameters
SqsPublisher<T>(IAmazonSQS sqsClient, string queueUrl) The SQS client instance and the queue URL to publish to
Method Description
PublishAsync(T message, CancellationToken ct) Serializes the message to JSON and sends it to the SQS queue
PublishBatchAsync(IEnumerable<T> messages, CancellationToken ct) Sends up to 10 messages in a single SQS batch request
DisposeAsync() Disposes the underlying IAmazonSQS client

SqsConsumer<T>

Constructor Parameters
SqsConsumer<T>(IAmazonSQS sqsClient, string queueUrl) The SQS client instance and the queue URL to consume from
Method Description
StartAsync(Func<T, CancellationToken, Task> handler, CancellationToken ct) Starts the background polling loop
StopAsync(CancellationToken ct) Cancels the polling loop and waits for completion
DisposeAsync() Disposes the underlying IAmazonSQS client

Requirements

  • .NET 9.0+
  • AWSSDK.SQS (installed automatically)

License

Apache-2.0

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.2 107 4/5/2026
3.0.1 105 4/3/2026
2.0.4 103 3/21/2026
2.0.3 92 3/21/2026
2.0.2 98 3/21/2026
2.0.1 100 3/19/2026
1.0.2 239 12/19/2025
1.0.1 264 12/19/2025
1.0.0 258 12/19/2025