Search code examples
c#concurrencyrabbitmqtask-parallel-libraryeasynetq

How to correctly use EasyNetQ in a multi-threaded environment


I'm learning about message brokers (specifically RabbitMQ) and I've started a project using EasyNetQ. From what I have read, the best practice is to use long-standing connections to the broker and end them at the end of your application. In my project, I am using EasyNetQ in a windows service. Would the best approach be to instantiate the connection (IBus) in the onStartMethod() and dispose of it during the onStop()? If so, if I instantiate some tasks running on various threads in the OnStartMethod() can each thread safely use the Bus instance to publish/subscribe? I couldn't find anything in the documentation. Thanks in advance.


Solution

  • That's all correct, keep one instance of IBus in your app, and it can be safely used from multiple threads.