MVFC.Messaging.RabbitMQ 3.0.2

dotnet add package MVFC.Messaging.RabbitMQ --version 3.0.2
                    
NuGet\Install-Package MVFC.Messaging.RabbitMQ -Version 3.0.2
                    
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.RabbitMQ" Version="3.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MVFC.Messaging.RabbitMQ" Version="3.0.2" />
                    
Directory.Packages.props
<PackageReference Include="MVFC.Messaging.RabbitMQ" />
                    
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.RabbitMQ --version 3.0.2
                    
#r "nuget: MVFC.Messaging.RabbitMQ, 3.0.2"
                    
#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.RabbitMQ@3.0.2
                    
#: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.RabbitMQ&version=3.0.2
                    
Install as a Cake Addin
#tool nuget:?package=MVFC.Messaging.RabbitMQ&version=3.0.2
                    
Install as a Cake Tool

MVFC.Messaging.RabbitMQ

πŸ‡§πŸ‡· Leia em PortuguΓͺs

CI codecov License Platform NuGet Version NuGet Downloads

A .NET messaging provider for RabbitMQ, built on top of MVFC.Messaging.Core. Provides RabbitMqPublisher<T> and RabbitMqConsumer<T> for publishing and consuming JSON-serialized messages in RabbitMQ queues with persistent delivery, QoS prefetch, and explicit acknowledgment.

Package

Package Downloads
MVFC.Messaging.RabbitMQ Downloads

Installation

dotnet add package MVFC.Messaging.RabbitMQ

This package depends on MVFC.Messaging.Core (installed automatically) and RabbitMQ.Client.

Configuration

Connection String

Both RabbitMqPublisher<T> and RabbitMqConsumer<T> use an async factory pattern (CreateAsync) and accept a connection string (AMQP URI) and a queue name.

amqp://guest:guest@localhost:5672/
amqp://user:password@rabbitmq-host:5672/vhost

Queue Configuration

The queue is automatically declared during creation with the following settings:

Setting Value Description
durable true Queue survives broker restarts
exclusive false Queue is not exclusive to the connection
autoDelete false Queue is not deleted when the last consumer disconnects

Consumer QoS

The consumer sets a prefetch count of 10, which means the broker delivers up to 10 unacknowledged messages to the consumer at a time.

appsettings.json Example

{
  "RabbitMQ": {
    "ConnectionString": "amqp://guest:guest@localhost:5672/",
    "QueueName": "orders"
  }
}
var connectionString = builder.Configuration["RabbitMQ:ConnectionString"]!;
var queueName = builder.Configuration["RabbitMQ:QueueName"]!;

Usage

Async Factory Pattern

Unlike other providers, RabbitMQ uses an async factory method instead of a regular constructor, because establishing the connection and declaring the queue are async operations:

// βœ… Correct β€” use CreateAsync
await using var publisher = await RabbitMqPublisher<OrderCreated>.CreateAsync(connectionString, queueName);
await using var consumer = await RabbitMqConsumer<OrderCreated>.CreateAsync(connectionString, queueName);

// ❌ Wrong β€” constructor is private
// var publisher = new RabbitMqPublisher<OrderCreated>(...);

Publishing a Single Message

using MVFC.Messaging.RabbitMQ.Rabbit;

var connectionString = "amqp://guest:guest@localhost:5672/";
var queueName = "orders";

await using var publisher = await RabbitMqPublisher<OrderCreated>.CreateAsync(connectionString, queueName);

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

Messages are serialized to JSON, encoded as UTF-8, and published with Persistent = true to survive broker restarts.

Publishing a Batch

Batch publishing sends each message sequentially using the same channel:

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 uses AsyncEventingBasicConsumer for event-driven message delivery with explicit acknowledgment:

using MVFC.Messaging.RabbitMQ.Rabbit;

var connectionString = "amqp://guest:guest@localhost:5672/";
var queueName = "orders";

await using var consumer = await RabbitMqConsumer<OrderCreated>.CreateAsync(connectionString, queueName);

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:

  • Uses AsyncEventingBasicConsumer for fully asynchronous message handling.
  • autoAck is set to false β€” messages must be explicitly acknowledged.
  • On successful handler execution, BasicAckAsync is called β€” the message is removed from the queue.
  • On handler exception, BasicNackAsync is called with requeue: true β€” the message returns to the queue for redelivery.
  • QoS prefetch count is 10 β€” the broker delivers up to 10 messages at a time before waiting for acks.

Complete Publish + Consume Example

using MVFC.Messaging.RabbitMQ.Rabbit;

var connectionString = "amqp://guest:guest@localhost:5672/";
var queueName = "orders";

await using var publisher = await RabbitMqPublisher<OrderCreated>.CreateAsync(connectionString, queueName);
await using var consumer = await RabbitMqConsumer<OrderCreated>.CreateAsync(connectionString, queueName);

// 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);

await Task.Delay(1000); // Wait for consumer initialization

// 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(10));

// Cleanup
await consumer.StopAsync();

API Reference

RabbitMqPublisher<T>

Factory Method Parameters
RabbitMqPublisher<T>.CreateAsync(string connectionString, string queueName) AMQP URI and queue name (queue is auto-declared)
Method Description
PublishAsync(T message, CancellationToken ct) Serializes to JSON, encodes as UTF-8, and publishes with persistence
PublishBatchAsync(IEnumerable<T> messages, CancellationToken ct) Publishes each message sequentially
DisposeAsync() Closes and disposes the channel and connection

RabbitMqConsumer<T>

Factory Method Parameters
RabbitMqConsumer<T>.CreateAsync(string connectionString, string queueName) AMQP URI and queue name (queue is auto-declared, QoS is configured)
Method Description
StartAsync(Func<T, CancellationToken, Task> handler, CancellationToken ct) Creates an async event consumer and starts consuming
StopAsync(CancellationToken ct) No-op (consumer stops when connection is disposed)
DisposeAsync() Closes and disposes the channel and connection

Requirements

  • .NET 9.0+
  • RabbitMQ.Client (installed automatically)
  • A running RabbitMQ server (or Docker: docker run -p 5672:5672 -p 15672:15672 rabbitmq:management)

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 116 4/5/2026
3.0.1 103 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 101 3/19/2026
1.0.2 244 12/19/2025
1.0.1 268 12/19/2025
1.0.0 262 12/19/2025