Managing Azure Service Bus Connections
There are many tutorials that go over the features of Azure Service bus and demonstrate how to publish messages but in my experience I haven’t seen many posts that demonstrate the most optimal approach for applications with long lived processes such as a web application.
The typical example I see online looks something like this:
There’s absolutely nothing wrong with the sample code provided by Microsoft however it can be a little miss leading if you don’t consider the context in which this code was written.
The sample application that MS used was a console app which has a very short lifetime. The application will start, create a new client and sender, send a batch of messages and finally dispose the client & sender. In this context the code works without any issues however if the sample code was used in something like a web application you’ll likely start to encounter performance issues. This is because the official recommendation from MS is that both the client and senders should be singletons (docs can be found here…