'ServiceBusSender - is it recommended to cache and reuse?
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?
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sean Feldman |
