NanoRabbit 0.0.3

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

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

NanoRabbit logo

NanoRabbit

NanoRabbit, A Lightweight RabbitMQ .NET API for .NET 6.

Installation

You can get NanoRabbit by grabbing the latest NuGet package.

Version

NanoRabbit RabbitMQ.Client
0.0.1 6.5.0
0.0.2 6.5.0
0.0.3 6.5.0

QuickStart

Note: NanoRabbit heavily relies on Naming Connections, Producers, Consumers.

Follow the codes below to start using NanoRabbit!

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();
pool.RegisterConnection(new ConnectOptions("Connection1")
{
    ConnectConfig = new()
    {
        HostName = "localhost",
        Port = 5672,
        UserName = "admin",
        Password = "admin",
        VirtualHost = "DATA"
    },
    ProducerConfigs = new List<ProducerConfig> 
    {
        new ProducerConfig("DataBasicQueueProducer")
        {
            ExchangeName = "BASIC.TOPIC",
            RoutingKey = "BASIC.KEY",
            Type = ExchangeType.Topic
        }
    },
    ConsumerConfigs = new List<ConsumerConfig>
    {
        new ConsumerConfig("DataBasicQueueConsumer")
        {
            QueueName = "BASIC_QUEUE"
        }
    }
});

Simple Publish

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

await Task.Run(async () =>
{
    while (true)
    {
        pool.SimplePublish<string>("Connection1", "DataBasicQueueProducer", "Hello from SimplePublish<T>()!");
        Console.WriteLine("Sent to RabbitMQ");
        await Task.Delay(1000);
    }
});

Receive messages

Instantiate a consumer in Program.cs:

var consumer = new BasicConsumer("Connection1", "DataBasicQueueConsumer", pool);
await Task.Run(async () =>
{
    while (true)
    {
        Console.WriteLine("Start receiving...");
        consumer.Receive();
        await Task.Delay(1000);
    }
});

Create your own consumer, for example: BasicConsumer.cs:

public class BasicConsumer : RabbitConsumer<string>
{
    public BasicConsumer(string connectionName, string consumerName, IRabbitPool pool) : base(connectionName, consumerName, pool)
    {
    }

    protected override void MessageHandler(string message)
    {
        Console.WriteLine($"Receive: {message}");
    }
}

DependencyInjection

Register IRabbitPool in Program.cs:

HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Services.AddRabbitPool(c =>
{
    c.Add(new ConnectOptions("Connection1")
    {
        ConnectConfig = new ConnectConfig
        {
            HostName = "localhost",
            Port = 5672,
            UserName = "admin",
            Password = "admin",
            VirtualHost = "DATA"
        },
        ProducerConfigs = new List<ProducerConfig> { 
            new ProducerConfig("DataBasicQueueProducer")
            {
                ExchangeName = "BASIC.TOPIC",
                RoutingKey = "BASIC.KEY",
                Type = ExchangeType.Topic
            }
        },
        ConsumerConfigs = new List<ConsumerConfig>
        {
            new ConsumerConfig("DataBasicQueueConsumer")
            {
                QueueName = "BASIC_QUEUE"
            }
        }
    });

    c.Add(new ConnectOptions("Connection2")
    {
        // ...
    });
});

Then, you can use IRabbitPool at anywhere, for example, create a publisher backgroungservice:

public class PublishService : BackgroundService
{
    private readonly IRabbitPool _rabbitPool;
    private readonly ILogger<PublishService> _logger;

    public PublishService(IRabbitPool rabbitPool, ILogger<PublishService> logger)
    {
        _rabbitPool = rabbitPool;
        _logger = logger;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            _rabbitPool.SimplePublish("Connection1", "DataBasicQueueProducer", "Hello from conn1");
            _logger.LogInformation("Conn 1");
            _rabbitPool.SimplePublish("Connection2", "HostBasicQueueProducer", "Hello from conn2");
            _logger.LogInformation("Conn 2");
            await Task.Delay(1000, stoppingToken);
        }
    }
}

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

  • DependencyInjection support
  • Logging support
  • ASP.NET support

Thanks

LICENSE

NanoRabbit is licensed under the Apache-2.0 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 was computed.  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 was computed.  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.9 68 7/11/2024
0.1.9-hotfix 56 7/15/2024
0.1.8 71 7/8/2024
0.1.7 69 7/7/2024
0.1.6 132 1/23/2024
0.1.6-hotfix 97 1/23/2024
0.1.5 89 1/22/2024
0.1.4 119 1/11/2024
0.1.3 89 1/10/2024
0.1.2 107 1/9/2024
0.1.1 103 1/9/2024
0.1.0 113 1/8/2024
0.0.9 127 12/27/2023
0.0.8 112 12/26/2023
0.0.7 129 9/8/2023
0.0.6 121 8/8/2023
0.0.5 134 7/31/2023
0.0.4 117 7/27/2023
0.0.3 162 7/20/2023
0.0.2 139 7/20/2023
0.0.1 123 7/19/2023