Reputation: 8763
We have a component that receives messages and posts them to the service bus.
I'm wondering if it recommended to cache and keep using the same ServiceBusSender or to create and use a new object every time.
_serviceBusClient = new ServiceBusClient(_serviceBus.ConnectionString);
_serviceBusSender = _serviceBusClient.CreateSender(Topic);
and in the dispose
_serviceBusSender?.CloseAsync()?.Wait();
_serviceBusSender = null;
_serviceBusClient?.DisposeAsync().AsTask()?.Wait();
_serviceBusClient = null; // should we be caching and re-using _serviceBusSender and _serviceBusClient?
Upvotes: 0
Views: 1287
Reputation: 26012
ServiceBusClient
should not be disposed. It keeps the connection object to the broker. Reestablishing connection to the broker is not a cheap operation. Creating senders and receivers with the same client is inexpensive and is ok.
Upvotes: 2