Skip to content

STARIONGROUP/Mercurio

Repository files navigation

Mercurio

A library to make RabbitMQ integration in .NET microservices seamless

Introduction

Mercurio provides RabbitMQ integration with common implementation of Messaging Client. This common implementation of messaging client reduce the need of code duplication on microservices implementation that requires to interacts with RabbitMQ.
It also eases the setup of ConnectionFactory and also to register multiple ConnectionFactory setup, using the IRabbitMqConnectionProvider.

Examples

Dependency Injection Registration

var serviceCollection = new ServiceCollection();

serviceCollection.AddRabbitMqConnectionProvider()
    .WithRabbitMqConnectionFactoryAsync("Primary", _ =>
    {
        var connectionFactory = new ConnectionFactory()
        {
            HostName = "localhost",
            Port =  5432
        };

        return Task.FromResult(connectionFactory);
    })
    .WithRabbitMqConnectionFactory("Secondary", _ =>
    {
        var connectionFactory = new ConnectionFactory()
        {
            HostName = "localhost",
            Port =  5433
        };

        return connectionFactory;
    })
    .WithDefaultJsonMessageSerializer();

var serviceProvider = serviceCollection.BuildServiceProvider();
var connectionProvider = serviceProvider.GetRequiredService<IRabbitMqConnectionProvider>();
var primaryConnection = await  connectionProvider.GetConnectionAsync("Primary");

Message Serialization

The MessageClientService requires to serialize and deserialize messages sent on RabbitMQ. For that, two interfaces are declared: IMessageDeserializerService and IMessageSerializerService.
A JSON basic implementation is already provided and can be registered using the .WithDefaultJsonMessageSerializer() extension method on the IServiceCollection.
This implementation provides basic JSON serialization using the System.Text.Json library.

MessageClientService

A base implementation of a MessageClientService is available. It defines base behavior to push and listen after messages on queue and exchange.
Following example expects to have a connection registered, the service registered as IMessageClientBaseService into the service collection and will use Direct Exchange.

Push Message

var messageClientService = serviceProvider.GetRequiredService<IMessageClientBaseService>();
var exchangeConfiguration = new DirectExchangeConfiguration("DirectQueue", "AnExchange", "SomeRouting");
await messageClientService.PushAsync("RegisteredConnection","A message to be sent",exchangeConfiguration);

Listen After Message

var messageClientService = serviceProvider.GetRequiredService<IMessageClientBaseService>();
var exchangeConfiguration = new DirectExchangeConfiguration("DirectQueue", "AnExchange", "SomeRouting");
var messageObservable = await messageClientService.ListenAsync<string>("RegisteredConnection", exchangeConfiguration);
messageObservable.Subscribe(message => Console.WriteLine(message));

Integration Tests

Before running any integration tests, it is required to have a running instance of RabbitMQ.
Please use this following command to run it :

docker run -d --name mercurio -p 15672:15672 -p 5672:5672 rabbitmq:4-management 

Code Quality

Quality Gate Status Code Smells Coverage Duplicated Lines (%) Lines of Code Maintainability Rating Reliability Rating Security Rating Technical Debt Vulnerabilities

Nuget

The Mercurio library is released as NuGet package and available from nuget.org.
NuGet Badge

About

A library to make RabbitMQ integration in .NET microservices seamless

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages