I've been dealing with a spate of Timeout's from TopicClient and I think it may be related to object lifetime and disposal.
I'm using the TopicClient
class from Microsoft.ServiceBus.Messaging
and reading this Best Practices Guide states
You should not close messaging factories or queue, topic, and subscription clients after you send a message, and then re-create them when you send the next message. Closing a messaging factory deletes the connection to the Service Bus service, and a new connection is established when recreating the factory.
This is confusing to me - this document doesn't specifically refer to TopicClient
but i am assuming it applies. Perhaps this assumption is incorrect?
Can I just store my TopicClient in a static member to avoid recreating the connection? Is there a better way to handle this? Is there some sort of connection pooling mechanism that I should use instead?
The documentation indicates that all static and instance members of the TopicClient are threadsafe.
"Any public static (Shared in Visual Basic) members of this type are thread safe. Instance members are also guaranteed to be thread safe."
I read the quote you included as that it does specifically include the TopicClient because it says "queue, topic and subscription clients". I read that as QueueClient, TopicClient and SubscriptionClient.
For messaging subsystems I tend to use a Gateway Pattern which can be used to handle the lifetime of the object necessary to work with the messaging subsystem. In your case the Gateway object would handle the lifetime of the TopicClient, or MessageSender/Receivers.
I would ask if you have seen the more generic MessageSender and MessageReceiver classes? Using these more generic objects when sending means the client code doing the sending doesn't have to know whether or not it is sending to a Topic or a Queue, it can just send to the address and not care.
Just to add another confirmation from MS that this is thread safe, from Best Practices for performance improvements using Service Bus Messaging:
You can safely use the QueueClient object for sending messages from concurrent asynchronous operations and multiple threads.
They don't mention TopicClient
explicitly, but would expect the same goes for TopicClient as for QueueClient
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With