Chd (Cleverly Handle Difficulty) packages are easy to use. This package contains RabbitMQ message queue abstractions and helpers for pub/sub patterns and reliable messaging in distributed systems.
License
—
Deps
4
Install Size
—
Vulns
✓ 0
Published
Feb 19, 2026
$ dotnet add package Chd.Library.MQChd (Cleverly Handle Difficulty) library helps you cleverly handle difficulty, write code quickly, and keep your application stable.
A message queue is a form of asynchronous service-to-service communication used in serverless and microservices architectures. Messages are stored on the queue until they are processed and deleted. Each message is processed only once, by a single consumer. Message queues can be used to decouple heavyweight processing, to buffer or batch work, and to smooth spiky workloads.
RabbitMQ is an open-source message broker software that implements the Advanced Message Queuing Protocol (AMQP). This middleware is a popular, reliable, and scalable platform for building realtime applications.
You must use .net core 9.0 or higher
We will create a container for the message queue. We will produce messages to the queue and consume them. We will use the RabitMQ for this purpose.
We will create a SampleClass and EmailMessage class for producing messages. We will create instance the SampleClass and set SerializableObject property to EmailMessage intance. We will produce the SampleClass instance to the queue. We will consume the SampleClass instance and get the EmailMessage instance from the SerializableObject property.
public class SampleClass
{
public object SerializableObject { get; set; }
public string Message { get; set; }
public string Description { get; set; }
}
public class EmailMessage :
{
public string To { get; set; }
public string Subject { get; set; }
public string Content { get; set; }
}
We will inject the RabitMQ and it's message producer into the project. We will use the AddMQ and AddMQProducer methods for this purpose. We will use the AddMQ method for injecting the RabitMQ into the project. If we needs message producing, we should use the Add MQProducer method for injecting the message producer.
var builder = WebApplication.CreateBuilder(args);
......
......
builder.Services.AddMQ(); // This adds the RabitMQ connection factory to the project. For only consume messages, you can use this method.
builder.Services.AddMQProducer<SampleClass>();// This adds the RabitMQ producer to the project. For producing messages, you can use this method.
We will create a controller for producing messages to the RabitMQ. We will use the Producer class for producing messages. We will use the SendMessage method for producing messages. We will set the first parameter of the SendMessage method to the SampleClass instance. We will set the second parameter of the SendMessage method to the boolean value. We will set the third parameter of the SendMessage method to the queue name. The other parameters are optional. We do not need to enter them. We may set the fourth parameter of the SendMessage method to the exchange name. We may set the fifth parameter of the SendMessage method to the routing key.
public class RabbitMQTestController : Controller
{
public Producer<SampleClass> Producer { get; }
public RabbitMQTestController(Producer<SampleClass> producer)
{
Producer = producer;
}
[Route("AddMessagesToRabbitMQ")]
[HttpGet]
public string? AddMessagesToRabbitMQ()//For testing on swagger or etc..
{
var emailMessage = new EmailMessage { To="to",Content="test rabbitMQ",Subject="test"};
Producer.SendMessage(new SampleClass { SerializableObject = emailMessage, Description = "Test1",Message= "xxx" },true,queueName:"testQueue");
return "Ok";
}
}
We will create a consumer service for consuming messages from the RabitMQ. We will create a class that inherits the ConsumerServiceBase class. We will override the OnMessageDelivered method for consuming messages. We will override the OnErrorOccured method for handling errors.
public class SampleConsumerService : ConsumerBase<SampleClass>
{
public SampleConsumerService(ConnectionFactory connectionFactory) : base(connectionFactory)
{
}
protected override string QueueName => "testQueue";
public override Task ReceiveAction(SampleClass message)
{
//Write your code here
return Task.CompletedTask;
}
}
we will add hosted service for consuming messages from the RabitMQ. We will use the AddHostedService method for adding the hosted service. We will use the AddSingleton method for injecting the SampleConsumerService class into the project.
For using this CronJobService, you must add the Chd.Library.Service package to your project. You can search the Chd.Library.Service package on the Nuget Package Manager and install it.
We set the CronJob attribute to the RabbitMqSamapleService. We set the value of the CronJob attribute to the cron expression. We override the Run method for consuming messages. We override the StartAsync method for starting the consumer service. We create an instance of the SampleConsumerService class in the StartAsync method. We call the Connect method of the SampleConsumerService class in the Run method.
```bash
```csharp
[CronJob("*/1 * * * *")]
public class RabbitMqSamapleService : CronJobService
{
public ConnectionFactory ConnectionFactory { get; }
public RabbitMqSamapleService(ConnectionFactory connectionFactory)
{
ConnectionFactory = connectionFactory;
}
public override void Run(CancellationToken cancellationToken)
{
if(sampleConsumerService.Status==false)
{
sampleConsumerService.Connect();
}
}
SampleConsumerService sampleConsumerService;
public override Task StartAsync(CancellationToken cancellationToken)
{
sampleConsumerService = new SampleConsumerService(ConnectionFactory);
sampleConsumerService.Connect();
return base.StartAsync(cancellationToken);
}
}
You must add code below in appsettings.json
"MQ": {
"Host": "localhost", //change it if you use different host
"UserName": "YOUR_USERNAME", //change it if you use different username
"Password": "YOUR_PASSWORD", //change it if you use different password
"Port": "5672" //change it if you use different port
}
We will run the application. We will call the AddMessagesToRabbitMQ method of the RabbitMQTestController class. We may use swagger or postman for calling the AddMessagesToRabbitMQ method. We will check the produced messages.
If our message is produced successfully, we will consume the message. Add break points to the ReceiveAction method of the SampleConsumerService class for checking the consumed messages. We will check the consumed messages.
See also the list of contributors who participated in this project.
Thank you for using my library.