After having looked into a message session based request-reply pattern, I decided to implement a request-reply model that does NOT use message sessions, but solely relies on the ReplyTo
and CorrelationId
message properties as described here:
Simple request/reply: A publisher sends a message into a queue and expects a reply from the message consumer. To receive the reply, the publisher owns a queue into which it expects replies to be delivered. The address of that queue is expressed in the ReplyTo property of the outbound message. When the consumer responds, it copies the MessageId of the handled message into the CorrelationId property of the reply message and delivers the message to the destination indicated by the ReplyTo property.
Now, the problem is that when service A is receiving messages from multiple services B, C, D,... etc., it cannot know beforehand which queue name will be in the ReplyTo
field. As a consequence, A must be able to create ServiceBusSender
instances dynamically for being able to write reply messages to various queues.
This kind of goes against the suggestion to create long-lasting ServiceBusSender
instances:
The Service Bus objects that interact with the service, such as ServiceBusClient, ServiceBusSender, ServiceBusReceiver, and ServiceBusProcessor, should be registered for dependency injection as singletons (or instantiated once and shared). [...] We recommend that you don't close or dispose these objects after sending or receiving each message.
So what's the best way do deal with the above conflictive requirements of dynamic vs. static ServiceBusSender
instance creation when also considering dependency injection?