Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiating multiple instance of Microsoft.Azure.ServiceBus.QueueClient in .NET Core

I'm looking for the best practices for creating multiple Microsoft.Azure.ServiceBus.QueueClient instances in .Net Core using Dependency Injection. Should instances be singleton, for example? I couldn't find any official guidance on this.

Currently I am resolving one instance as a singleton, but a new requirement has necessitated that I instantiate multiple QueueClient instances for different queues using the same connection string.

The concern is connection pooling and lifetime, and how this is/should be managed. I've found mentions of MessagingFactory in .NET Framework, but not much info about the equivalent (if any) in .NET Core.

like image 832
killswitch Avatar asked Mar 15 '26 16:03

killswitch


1 Answers

.NET Framework version of the client was using MessagingFactoring as a mechanism for connection pooling. Each client created using the same factory would re-use the same connection object.

With .NET Standard client this is no longer the case. You get to choose wherever you want to pool the connection or not. If you construct all of your queue clients using a connection string, you will create new connections each time. It's both costly and requires resources. If you construct your queue clients using the same ServiceBusConnection object, then you'll re-use the same connection and will not pay the cost of re-establishing a connection each time.

Note that whenever you reuse the object, you need to verify that the underlying connection is still opened. If it's not, you will need to create a new one by specifying the connection string or instantiating a new connection object using the connection string you have.

like image 101
Sean Feldman Avatar answered Mar 18 '26 20:03

Sean Feldman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!