Automated dead-letter queue handling for EasyNetQ [RabbitMQ]
Tags: rabbitmq, easynetq, deadletter, dotnet
Published: 2022-04-19 06:23:12

**📃What is EasyNetQ?**
EasyNetQ is designed to make publishing and subscribing with RabbitMQ as easy as possible. of course, you can always use the RabbitMQ client to do this, but it brings lots of complexity of maintenance cumbersome to your application.
Digging into this library is out of the scope of this article, you can read more about this incredibly simple library [Here](https://easynetq.com/).

---

**📃What is the dead-letter queue?**


![Dead-letter rabbitmq and easyNetQ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w52eqrdcl16u6aafk6wb.png)
Messages from a queue can be "dead-lettered"; that is, republished to an exchange when any of the following events occur:

- The message is negatively acknowledged by a consumer using basic.reject or basic.nack with requeue parameter set to false.

- The message expires due to per-message TTL

- The message is dropped because its queue exceeded a length limit.

---

**🔔EasyNetQ weakness!!**
Honestly. it's not a weakness, EasyNetQ tries to keep the implementation as simple as possible, because of this principle you cannot find so many features which already exist in the RabbitMQ client in this library.

But one of the biggest issues that a developer perhaps faces during the implementation of RabbitMQ functionality is dead-letter management.

EasyNetQ does not support automatic dead-letter definition. It means in case any of the conditions are mentioned, the failed message will be moved to the default error queue of RabbitMQ.

---

**❓What's wrong with one Error queue?**
Good question! dealing with different types of failed messages and monitoring the failed messages for different queues is not possible in this case. It means there should be a different dead-letter queue for each discrete queue.

---

**🌱Solution**

![EasyDeadLetter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0pjusgn8f59uap1u3vz3.png)
Here, I have implemented a Nuget package (EasyDeadLetter) for this purpose, which can be easily implemented with the minimum changes in any project.

All you need to do is follow the four steps :
1- First of all, Decorate your class object with **QeueuAttribute**

{% gist https://gist.github.com/MajeQafouri/a8c58fa7bfdf2657416a088dafdc89ce %}

2- The second step is to define your dead-letter queue with the same QueueAttribute and also inherit the dead-letter object from the Main object class.
{% gist https://gist.github.com/MajeQafouri/61b69f9795b0127973c861bb21297f74 %}

3- Now, it's time to decorate your main queue object with the EasyDeadLetter attribute and set the type of dead-letter queue.
{% gist https://gist.github.com/MajeQafouri/30e55712cd9fbaf54b1d9915d2eea8b4 %}

4- In the final step, you need to register **EasyDeadLetterStrategy** as the default error handler (**IConsumerErrorStrategy**).
{% gist https://gist.github.com/MajeQafouri/ae91dad6d406743f1a542351d65ef82e %}

That's all. from now on any failed message will be moved to the related dead-letter queue.

---

**📑Resources**
[NuGet Package](https://www.nuget.org/packages/EasyDeadLetterStrategy/)

{% github MajeQafouri/EasyDeadLetter %}

 

🤞If you want to support me, please **share** this article with your community.

Follow me on [Linkedin](https://www.linkedin.com/in/majid-qafouri)