Uentee
Uentee

Reputation: 883

Is there a way to wait for the reception of a rabbitmq message?

I have a use case where I need my controller action to wait for the reception of a specific rabbitmq message so I can return the result to the client, this message would come from a separate worker performing a certain task.

My api project and the worker project are separated and rabbitmq bus is the only intermediary between them.

EDIT: This is the current Scenario:

  1. Client sends request to the web api to ask for let's call it 'DATA'
  2. The web api publishes a Message-A through rabbitmq
  3. A separate service project handles the published Message-A, does some work, and publishes a new Message-B that contains the result of that work which we called 'DATA'
  4. Here is the problem: My web api controller have to return the results contained in Message-B, so the controller action should wait for that message before returning to the client

Upvotes: 2

Views: 1724

Answers (1)

Paulo Morgado
Paulo Morgado

Reputation: 14836

You need to use a TaskCompletionSource<T>.

You need to subscribe to the reply messages and, if it's the reply you're waiting for, set the result of the task completion source.

Then await the task of the task completion source.

Upvotes: 2

Related Questions