We have an API hosted on Windows Azure in a web role with 2 instances that takes in requests, validates them then adds them to an Azure Service Bus Queue.
Recently we've started load testing this and found that our current code throws the below exception:
Could not add command to the command queue Exception: Microsoft.ServiceBus.Messaging.QuotaExceededException: The number of maximumallowed connections have been reached or exceeded for Queue Number of active connections: 100, Maximum allowed connections: 100.
Would the below client code keep a single connection open assuming we maintained a single instance of the class? I am trying to ascertain whether the issue is with ServiceBusClient code or our dependency registration initialising a new client for each request.
public class ServiceBusClient : IDisposable
{
#region Constants and Fields
protected readonly NamespaceManager NamespaceManager;
private const int DEFAULT_LOCK_DURATION_IN_SECONDS = 300; // 5 minutes
private const string SERVICE_BUS_CONNECTION_STRING_KEY
= "service.bus.connection.string";
private readonly IMessageBodySerializer _messageBodySerializer;
private readonly MessagingFactory _messagingFactory;
private readonly QueueClient _queueClient;
private readonly string _queueName;
private readonly ISettingsManager _settingsManager;
#endregion
#region Constructors and Destructors
public ServiceBusClient(
ISettingsManager settingsManager,
IMessageBodySerializer messageBodySerializer, s
tring queueName)
{
_settingsManager = settingsManager;
_messageBodySerializer = messageBodySerializer;
var connectionString = _settingsManager.GetSetting<string>(
SERVICE_BUS_CONNECTION_STRING_KEY);
NamespaceManager =
NamespaceManager.CreateFromConnectionString(connectionString);
_messagingFactory =
MessagingFactory.CreateFromConnectionString(connectionString);
_queueName = queueName;
_queueClient = GetOrCreateQueue();
}
#endregion
#region Public Methods and Operators
public virtual void SendMessage(object bodyObject)
{
var brokeredMessage =
_messageBodySerializer.SerializeMessageBody(bodyObject);
_queueClient.Send(brokeredMessage);
}
public void Dispose()
{
_messagingFactory.Close();
}
#endregion
#region Methods
private QueueClient GetOrCreateQueue()
{
QueueDescription queue;
if (!NamespaceManager.QueueExists(_queueName))
{
var queueToCreate = new QueueDescription(_queueName)
{
LockDuration = TimeSpan.FromSeconds(
DEFAULT_LOCK_DURATION_IN_SECONDS)
};
queue = NamespaceManager.CreateQueue(queueToCreate);
}
else
{
queue = NamespaceManager.GetQueue(_queueName);
}
return _messagingFactory.CreateQueueClient(
queue.Path,
ReceiveMode.PeekLock);
}
#endregion
}
As an extension to this; if the above code does keep an active connection open; would using the singleton pattern to store an instance of the ServiceBusClient for each instance of the API be dangerous? Or does the Azure SDK handle closed connections internally?