Skip to content
A lightweight .NET standard messaging library for easily deal with any message broker, like RabbitMQ, ActiveMQ, AmazonSQS, AzureQueue, ...
C#
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
samples
src
tests Merge branch 'master' into master-1680927 Jul 12, 2019
.gitignore initial commit Jul 2, 2019
Benner.Messaging.sln
DETAILS.md renaming azure transport and config, updating nuget pack Jul 18, 2019
DETALHES.md renaming azure transport and config, updating nuget pack Jul 18, 2019
LEIAME.md
LICENSE
README.md docs Jul 19, 2019
azure-pipelines.yml
key.snk

README.md

Benner.Messaging

Benner.Messaging is a .NET Standard lightweight messaging library to deal with any message broker with ease. Benner.Messaging supports RabbitMQ, ActiveMQ, Amazon SQS and Azure Queue. It is free and open-source under MIT License.

Build Status

Branch Status
master Build Status

Nuget Package

Package Name .NET Framework .NET Standard
Benner.Messaging 4.6.1 2.0

One Messaging API to rule them all!

We provide an extremely simple and intuitive API for sending and receiving messages to either RabbitMQ, ActiveMQ, Amazon SQS or Azure Queue:

Messaging.Enqueue("queue-name", "hello world!");
var message = Messaging.Dequeue("queue-name");

Yep, you got that right! You code just once, on an extremly simple way, and run over any message broker. No vendor lock-in, no code refactoring. All the magic relies on configuration (file or injected code).

Behavior matters

It's important to notice that Messaging API was born to work on a specific way:

  • First in, first out
  • One producer sends
  • Only one consumer receives
  • If consumer fails at receiving or processing, the message returns to queue
  • The message will not be lost

Enqueue and Dequeue operations were designed to ensure that a sent message by the sender successfully arrives on the receiver. That means that we pursuit Publisher Confirms and Consumer Acknoledgement approach across any supported broker.

Get Started

Sending message

Create a new project:

dotnet new console -n producer
cd producer

Add Benner.Messaging and open the project on vscode:

dotnet add package benner.messaging
code .

Add using and just send a message to some queue:

using Benner.Messaging;
Messaging.Enqueue("queue-name", "hello world!");

That's it! dotnet run it and you will get:

Messaging config not found

Brokers configuration

Well, you need a messaging.config file, like that (don't worry, we will get deeper on configuration ahead):

<MessagingConfigSection>
   <!-- Benner.Messaging is multi broker, chose the default -->
   <brokerList default="RabbitMQ">
      <broker name="AzureQueue" type="Benner.Messaging.AzureQueueConfig, Benner.Messaging">
         <add key="ConnectionString" value="DefaultEndpointsProtocol=https;AccountName=accountName;AccountKey=accountKey;EndpointSuffix=core.windows.net" />
         <add key="InvisibilityTime" value="15" />
      </broker>
      <broker name="RabbitMQ" type="Benner.Messaging.RabbitMQConfig, Benner.Messaging">
         <add key="Hostname" value="servername" />
         <add key="Username" value="username" />
         <add key="Password" value="password" />
         <add key="Port" value="port" />
      </broker>
      <broker name="AmazonSQS" type="Benner.Messaging.AmazonSQSConfig, Benner.Messaging">
         <add key="InvisibilityTime" value="15" />
		 <add key="AccessKeyId" value="idChaveDeAcesso" />
		 <add key="SecretAccessKey" value="chaveDeAcessoSecreta" />
      </broker>
      <broker name="ActiveMQ" type="Benner.Messaging.ActiveMQConfig, Benner.Messaging">
         <add key="Hostname" value="servername" />
         <add key="Username" value="username" />
         <add key="Password" value="password" />
         <add key="Port" value="port" />
      </broker>
   </brokerList>
   <!-- specify only queues that should use other than default broker -->
   <queues>
      <queue name="queue-name" broker="broker-name" />
   </queues>
</MessagingConfigSection>

You can also inject config through code:

var config = new MessagingConfigBuilder()
    .WithRabbitMQBroker("RabbitMQ", "servername", setAsDefault: true)
    .Create();

Messaging.Enqueue("queue-name", "hello world!", config);

That's it! dotnet run it and you will get:

Unable to connect to RabbitMQ server

Provisioning a broker

Well... you need, in this case, a RabbitMQ runnig according to your configuration.

Thankfully humanity evolved and we have Docker containers to help us out. So, let's just run it!

docker run -d -v rabbitmq_data:/var/lib/rabbitmq -p 15672:15672 -p 15671:15671 -p 5672:5672 -p 5671:5671 rabbitmq:3.7-management

Now we are good to go! dotnet run it. No errors? Great! Now what?

Check broker management console

On your browser, access http://servername:15672/#/queues (user guest, password guest) to manage Rabbit and you'll be able to see your queue with your messages inside it.

Receive message

Create a new project:

dotnet new console -n consumer
cd consumer

Add Benner.Messaging and open the project on vscode:

dotnet add package benner.messaging
code .

Add using and just receive a message from the queue, not forgeting configuration:

using Benner.Messaging;

var config = new MessagingConfigBuilder()
    .WithRabbitMQBroker("RabbitMQ", "servername", setAsDefault: true)
    .Create();

var message = Messaging.Dequeue("queue-name", config);
Console.Write(message);

That's it! dotnet run it and you receive:

hello world! 

Now let's do it insanelly

Change consumer code to:

using Benner.Messaging;

var config = new MessagingConfigBuilder()
    .WithRabbitMQBroker("RabbitMQ", "servername", setAsDefault: true)
    .Create();

using (var client = new Messaging(config))
{
    client.StartListening("queue-name", (e) =>
    {
        // Print the message
        Console.WriteLine(e.AsString);
        return true;
    });
    // Stand-by the application so it can keep listening
    Console.ReadKey();
}

Ok, dotnet run it 3 times so you will have 3 consumers simultaneously

Change producer code to:

using Benner.Messaging;

var config = new MessagingConfigBuilder()
    .WithRabbitMQBroker("RabbitMQ", "servername", setAsDefault: true)
    .Create();

// Create new instance of messaging
using (var client = new Messaging(config))
{
    // Sending 1000 messages
    for (int i = 1; i <= 1000; i++)
        client.EnqueueMessage("queue-name", "hello world #" + i);
}

Done, dotnet run it and see what happens.

What's next

Check out get started samples here.

Or, read more details here.

You can’t perform that action at this time.