Rabbit MQ thread safety in an IHostedService

79 views Asked by At

I have three c# classes.

A rabbitmq subscriber has a connect() method, subscribe(Action messageHandler) method, and an Ack(ulong messageId) method. This class is instantiated as a singleton and passed to the following two classes via DI

An IHostedService that calls connect() and subscribe() on the rabbitmq subscriber. It also has a reference to a workflow class, a singleton passed in via DI. This hosted service never acknowledges a message; it just passes the message along to the workflow class when it is received from the queue.

The third class is a workflow class that takes the message from the IHostedService and passes it through a series of TPL dataflow blocks, the last of which calls acknowledge on the rabbitmq subscriber. The tpl dataflow pipeline never completes since its purpose is to stay alive with the IHostedService. The IHostedService does not have any knowledge of a message making it through the workflow, this is why the workflow class has a reference to the rabbit mq subscriber. It needs to ack the message when processing is complete.

The rabbit mq documentation specified that the underlying channel should not be used by more than one thread. How could I change my design such that I adhere to the one thread rule for rabbit mq?

Here is the documentation.

https://www.rabbitmq.com/dotnet-api-guide.html#concurrency

0

There are 0 answers