NanoRabbit 0.0.7

There is a newer version of this package available.
See the version list below for details.
dotnet add package NanoRabbit --version 0.0.7
NuGet\Install-Package NanoRabbit -Version 0.0.7
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="NanoRabbit" Version="0.0.7" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NanoRabbit --version 0.0.7
#r "nuget: NanoRabbit, 0.0.7"
#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.
// Install NanoRabbit as a Cake Addin
#addin nuget:?package=NanoRabbit&version=0.0.7

// Install NanoRabbit as a Cake Tool
#tool nuget:?package=NanoRabbit&version=0.0.7

NanoRabbit logo

NuGet Nuget Downloads License codebeat badge

About

NanoRabbit, A Lightweight RabbitMQ .NET API for .NET 6 and up, which makes a simple way to manage Multiple connections, producers, consumers, and easy to use.

NanoRabbit is under development! Please note that some APIs may change their names or usage!

Building

Branch Building Status
master build
dev build

Features

  • Customize the name of connections, producers, consumers.
  • Dependency injection available.
  • Multiple connections, producers, and consumers can be created.

Installation

You can get NanoRabbit by grabbing the latest NuGet package.

See Wiki for more details.

Version

NanoRabbit RabbitMQ.Client .NET
0.0.1, 0.0.2, 0.0.3, 0.0.4, 0.0.5, 0.0.6, 0.0.7 6.5.0 6.0, 7.0, 8.0

Document

The NanoRabbit Document is at NanoRabbit Wiki.

QuickStart

NanoRabbit is designed as a library depends on NAMING Connections, Producers, Consumers. So it's important to set a UNIQUE NAME for each Connections, Producers, Consumers.

For more, please visit the Examples.

Register a Connection

Register a RabbitMQ Connection by instantiating RabbitPool, and configure the producer and consumer.

var pool = new RabbitPool(config => { config.EnableLogging = true; });
pool.RegisterConnection(new ConnectOptions("Connection1", option =>
{
    option.ConnectConfig = new(config =>
    {
        config.HostName = "localhost";
        config.Port = 5672;
        config.UserName = "admin";
        config.Password = "admin";
        config.VirtualHost = "FooHost";
    });
    option.ProducerConfigs = new List<ProducerConfig>
    {
        new ProducerConfig("FooFirstQueueProducer", c =>
        {
            c.ExchangeName = "FooTopic";
            c.RoutingKey = "FooFirstKey";
            c.Type = ExchangeType.Topic;
        })
    };
    option.ConsumerConfigs = new List<ConsumerConfig>
    {
        new ConsumerConfig("FooFirstQueueConsumer", c => { c.QueueName = "FooFirstQueue"; })
    };
}));

Simple Publish

After registering the RabbitPool, you can simply publish a message by calling NanoPublish<T>().

Task publishTask = Task.Run(() =>
{
    while (true)
    {
        pool.NanoPublish<string>("Connection1", "FooFirstQueueProducer", "Hello from SimplePublish<T>()!");
        Console.WriteLine("Sent to RabbitMQ");
        Thread.Sleep(1000);
    }
});
Task.WaitAll(publishTask);

There is also a easy-to-use RabbitProducer, which used to publish messages without ConnectionName and ProducerConfig, for more, read Wiki.

Simple Consume

After registering the RabbitPool, you can simply consume a message by calling NanoConsume<T>().

Task consumeTask = Task.Run(() =>
{
    while (true)
    {
        pool.NanoConsume<string>("Connection1", "FooFirstQueueConsumer",
            msg => { Console.WriteLine($"Received: {msg}"); });
        Thread.Sleep(1000);
    }
});
Task.WaitAll(consumeTask);

There is also a easy-to-use RabbitConsumer, which used to consume messages without ConnectionName and ProducerConfig, for more, read Wiki.

Forward messages

Sometimes we have to consume messages from Foo RabbitMQ and publish the same message to Bar RabbitMQ, NanoRabbit provides a simple method to forward message, using the method called NanoForward<T>().

Task forwardTask = Task.Run(() =>
{
     while (true)
     {
         pool.NanoForward<string>("Connection1", "FooFirstQueueConsumer", "Connection2", "FooQueueProducer");
         Thread.Sleep(1000);
     }
});
Task.WaitAll(forwardTask);

DependencyInjection

Register IRabbitPool in Program.cs:

HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);

// Configure the RabbitMQ Connection
builder.Services.AddRabbitPool(
    globalConfig => { globalConfig.EnableLogging = true; },
    c =>
    {
        c.Add(new ConnectOptions("Connection1", option =>
        {
            option.ConnectConfig = new(config =>
            {
                config.HostName = "localhost";
                config.Port = 5672;
                config.UserName = "admin";
                config.Password = "admin";
                config.VirtualHost = "FooHost";
            });
            option.ProducerConfigs = new List<ProducerConfig>
            {
                new ProducerConfig("FooFirstQueueProducer", c =>
                {
                    c.ExchangeName = "FooTopic";
                    c.RoutingKey = "FooFirstKey";
                    c.Type = ExchangeType.Topic;
                })
            };
            option.ConsumerConfigs = new List<ConsumerConfig>
            {
                new ConsumerConfig("FooFirstQueueConsumer", c => { c.QueueName = "FooFirstQueue"; })
            };
        }));

        c.Add(new ConnectOptions("Connection2", option =>
        {
            // ...
        }));
    });

Then, you can use IRabbitPool at anywhere.

More DI Usage at Wiki.

Contributing

  1. Fork this repository.
  2. Create a new branch in you current repos from the dev branch.
  3. Push commits and create a Pull Request (PR) to NanoRabbit.

Todo

  • Basic Consume & Publish support
  • DependencyInjection support
  • Logging support
  • Forward messages
  • Using Task in Consumers and Producers
  • ASP.NET support
  • Exchange Configurations
  • .NET 7 support
  • .NET 8 support
  • RabbitMQ reconnecting

Thanks

License

NanoRabbit is licensed under the MIT license.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.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
0.1.6 126 1/23/2024
0.1.6-hotfix 85 1/23/2024
0.1.5 79 1/22/2024
0.1.4 116 1/11/2024
0.1.3 87 1/10/2024
0.1.2 103 1/9/2024
0.1.1 99 1/9/2024
0.1.0 109 1/8/2024
0.0.9 120 12/27/2023
0.0.8 105 12/26/2023
0.0.7 127 9/8/2023
0.0.6 114 8/8/2023
0.0.5 133 7/31/2023
0.0.4 116 7/27/2023
0.0.3 160 7/20/2023
0.0.2 136 7/20/2023
0.0.1 122 7/19/2023