GithubHelp home page GithubHelp logo

lambda3 / simple.bus Goto Github PK

View Code? Open in Web Editor NEW
16.0 3.0 3.0 50 KB

A simple way to receive and send message to Azure Service Bus and RabbitMq

License: MIT License

C# 100.00%
dotnet-core service-bus-topic azure-service-bus rabbitmq rabbitmq-client

simple.bus's Introduction

Simple Bus

A simple way to receive and send message to Azure Service Bus and RabbitMq

Installation

You can install through nuget.

Configuration

Receive

  • Azure Service Bus

You should use the class ReceiverConfigurationAzureServiceBus to configure service bus.

services
    .AddBusReceiverFor<YourMessage>(builder => builder
        .WithMessageHandler(message =>
        {
            Console.WriteLine($"Message received {message}");
            return Task.Completed;
        })
        .WithLogger(logger)
        .WithAzureServiceBus(receiverConfigurationAzureServiceBus));
  • RabbitMQ

You should use the class ReceiverConfigurationRabbitMQ to configure rabbitMQ.

services
    .AddBusReceiverFor<YourMessage>(builder => builder
        .WithMessageHandler(message =>
        {
            Console.WriteLine($"Receive message for rabbit mq: {message.Nome}");
            return Task.CompletedTask;
        })
        .WithRabbitMQ(handlerConfigurationRabbitMQ));

After that, you can inject ReceiverBuilderFor<YourMessage>

You must call Start for IReceiverFor<YourMessage> to start listening

You must call Stop for IReceiverFor<YourMessage> to stop listening

Send

  • RabbitMQ
var sender = new SenderPipelineBuilderFor<YourMessage>().WithRabbitMq(credentials, exchange).Build();
  • Azure Service Bus
var sender = new SenderPipelineBuilderFor<YourMessage>().WithAzureServiceBus(connectionString, topicName).Build();

or

services.AddBusSenderFor<YourMessage>(builder => builder.WithAzureServiceBus(connectionString, topicName));

Samples

There are four samples:

  • Samples/Simple.Bus.Sample.Producer.AzureServiceBus
  • Samples/Simple.Bus.Sample.Producer.RabbitMQ
  • Samples/Simple.Bus.Sample.Receiver
  • Samples/Simple.Bus.ReceiverDeadLetter

Try out the samples

RabbitMQ Producer

Start a container in a container with:

docker run -d --hostname my-rabbit --name some-rabbit -p 15672:15672 rabbitmq:3.8-management

You can read more about the RabbitMQ image on Docker Hub.

You can access http://localhost:15672/ and log into the management page with user guest and password guest. There, go to Exchanges and create an exchange with name exchange-message, then go to Queues and create a queue named queue-message, then go back to exchanges and click on the exchange-message exchange, click on bindings and add a binding to the queue-message queue.

On the terminal go to the directory for the Simple.Bus.Sample.Producer.RabbitMQ project and run it with:

dotnet run

The console app will prompt you to enter a text and a message will be created for it, create a few messages.

To see the messages go to the queue-message, click on the Get messages header, and click on the Get Message(s) button and you will be able to see the messages you sent from the terminal.

Azure Service Bus Producer

Start by creating the Azure Service Bus namespace, topic and subscription using Azure CLI:

az group create --name sb-sample --location brazilsouth
az servicebus namespace create --name sample-ns --resource-group sb-sample --location brazilsouth --sku Standard
az servicebus topic create --name topic-message --namespace-name sample-ns --resource-group sb-sample
az servicebus topic subscription create --name simple-bus-consumer --topic-name topic-message --namespace-name sample-ns --resource-group sb-sample

Get the connection string with:

az servicebus namespace authorization-rule keys list --namespace-name sample-ns --resource-group sb-sample --name RootManageSharedAccessKey --query primaryConnectionString -o tsv

Update the connection string on Program.cs in the Simple.Bus.Sample.Producer.AzureServiceBus project.

Use the Simple.Bus.Sample.Receiver project to read the message. Or you could use the Service Bus Explorer from Azure Portal to peek at the messages, or the Service Bus Explorer application.

Receiver (Azure Service Bus and RabbitMQ)

Look at how to configure the Azure Service Bus on the previous topic.

Update the appsettings.Development.json file to include the connection string.

Run the application with:

cd Samples/Simple.Bus.Sample.Receiver
dotnet run

Or you can build and run the container with the following command. Run it from the project root directory (where the .sln file is):

docker build -t receiver -f .\Samples\Simple.Bus.Sample.Receiver\Dockerfile .
docker run --rm -ti receiver bash

To run the dead letter sample use the same process, but substitute for the Simple.Bus.Sample.Receiver.DeadLetter directory.

Extensions

You can extend Serialization, Cryptography, Pipeline and Logging.

Contributing

The main supported IDE for development is Visual Studio 2019/2022.

Questions, comments, bug reports, and pull requests are all welcome. Bug reports that include steps to reproduce (including code) are preferred. Even better, make them in the form of pull requests. Before you start to work on an existing issue, check if it is not assigned to anyone yet, and if it is, talk to that person.

License

This software is open source, licensed under the MIT License. See LICENSE for details. Check out the terms of the license before you contribute, fork, copy or do anything with the code. If you decide to contribute you agree to grant copyright of all your contribution to this project and agree to mention clearly if do not agree to these terms. Your work will be licensed with the project at MIT, along the rest of the code.

simple.bus's People

Contributors

giggio avatar willsbctm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

simple.bus's Issues

Expose Bus properties

For example: UseAzureServiceBus should return a classe that inherits from ReceiverBuilderFor and is specific for SB, and allows me to configure it directly.
Or UseAzureServiceBus exposes a builder that exposes the SB primitives.

Same thing for RabbitMQ.

Create automated tests

Unit and integration would be cool, but at least unit tests for now. Integration would be a bit difficult, specially for Azure Service Bus.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.