Joseco.Communication.External.RabbitMQ 1.0.2

dotnet add package Joseco.Communication.External.RabbitMQ --version 1.0.2
                    
NuGet\Install-Package Joseco.Communication.External.RabbitMQ -Version 1.0.2
                    
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="Joseco.Communication.External.RabbitMQ" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Joseco.Communication.External.RabbitMQ" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Joseco.Communication.External.RabbitMQ" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Joseco.Communication.External.RabbitMQ --version 1.0.2
                    
#r "nuget: Joseco.Communication.External.RabbitMQ, 1.0.2"
                    
#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.
#:package Joseco.Communication.External.RabbitMQ@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Joseco.Communication.External.RabbitMQ&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Joseco.Communication.External.RabbitMQ&version=1.0.2
                    
Install as a Cake Tool

Joseco.Communication

This is a C# library for communication between systems providing a simple and efficient way to send and receive messages between systems using asynchronous messages using a message broker like RabbitMQ.

Initially, the only broker supported is RabbitMQ, but the library is designed to be extensible, allowing for the addition of other brokers in the future.

Installation

You can install the library via NuGet Package Manager Console:

Install-Package Joseco.Communication.External.RabbitMQ

or via .NET CLI:

dotnet add package Joseco.Communication.External.RabbitMQ

Using Contract-Only Package

If you only need the contract definitions and not the borker implementation, you can install the Joseco.Communication.External.Contracts package:

Install-Package Joseco.Communication.External.Contracts

or via .NET CLI:

dotnet add package Joseco.Communication.External.Contracts

This package contains the abstractions implemented by the Joseco.Communication.External.RabbitMQ package wich includes:

  • IntegrationMessage Represents a message that can be sent o received from the message broker.
  • IExternalPublisher Represents the publisher interface for sending messages to the message broker.
  • IIntegrationMessageConsumer Represents the consumer interface for receiving messages from the message broker.

This useful in scenarios when you need to separate assembly/project from the message broker implementation. This allows you to share the contracts across multiple projects without needing to reference the entire Joseco.Communication.External.RabbitMQ package.

Usage

Joseco.Communication.External.RabbitMQ allows you to send and receive messages using RabbitMQ as the message broker. The library provides a simple and efficient way to send and receive messages between systems using asynchronous messages.

To configure the RabbitMQ implementation, you need to register the services in your dependency injection container.


RabbitMqSettings settings = new RabbitMqSettings()
{
	Host = "host-address",
    UserName = "rabbitMqUser",
    Password = "rabbitMqPassword"
    VirtualHost = "/"
};

services.AddJosecoRabbitMq(settings);

Also, you can register the HealthCheck service to monitor the health of the RabbitMQ connection. This is useful for ensuring that your application can communicate with the message broker and handle any issues that may arise.

services.AddHealthChecks()
	.AddRabbitMqHealthCheck();

It's important to register the RabbitMQ services before registering the HealthCheck.

Sending Messages

To send a message, you need to create your own message class that inherits from the IntegrationMessage abstract record. This message class should contain the properties that you want to send in the message.


public class MyMessage : IntegrationMessage
{
	public string Property1 { get; set; }
	public int Property2 { get; set; }
}

IExternalPublisher is the interface used to send messages to the message broker. It provides a method PublishAsync that takes an IntegrationMessage object as a parameter. To use it, you can injected the IExternalPublisher interface into your class and call the PublishAsync method to send a message.


public class MyService
{
	private readonly IExternalPublisher _publisher;

	public MyService(IExternalPublisher publisher)
	{
		_publisher = publisher;
	}
	public async Task SendMessageAsync()
	{
		var message = new MyMessage
		{
			Property1 = "Hello",
			Property2 = 123
		};

		// By default, the destination always is an Exchange.
		// The destinationName parameter is optinal. If you don't pass any value or pass a null value, the destination name will be the same as the Message type name using the kebab case format.
		var destinationName = "my-exchange-name"; // This parameter is 
		
		var declareDestinationFirst = true; // Set to true if you want to declare the exchange before sending the message. This parameter is optional. The default value is false.
		
		await _publisher.PublishAsync(message, destinationName, declareDestinationFirst);


	}
}

Receiving Messages

To receive messages, you need to create a consumer class that implements the IIntegrationMessageConsumer<T> interface. This interface provides a method HandleAsync that takes an IntegrationMessage object as a parameter that containts the message recieved from the Broker.

public class MyMessageConsumer : IIntegrationMessageConsumer<MyMessage>
{
	public Task HandleAsync(MyMessage message)
	{
		// Handle the message here
		Console.WriteLine($"Received message: {message.Property1}, {message.Property2}");
		return Task.CompletedTask;
	}
}

To register the consumer, you need to use the AddIntegrationMessageConsumer method in your dependency injection container. This method takes the consumer type and the queue name as parameters.

services.AddRabbitMqConsumer<MyMessage, MyMessageConsumer>("my-queue-name");
Product Compatible and additional computed target framework versions.
.NET 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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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
1.0.2 226 5/30/2025
1.0.1 238 5/14/2025
1.0.0 201 5/3/2025