title: Azure Service Bus Client Connection Persistence
body: <p>I have below a basic wrapper around the Azure Service Bus code that we will be using within a worker role.  This <code>ServiceBusClient</code> will be instantiated each time the worker role is run; then used to access the queue until there are no remaining items left to enumerate through.</p>
  
  <pre><code>public class ServiceBusClient : IDisposable, IServiceBusClient
  {
      private const int DEFAULT_WAIT_TIME_IN_SECONDS = 120;
  
      private const string SERVICE_BUS_CONNECTION_STRING_KEY = "service.bus.connection.string";
  
      private readonly MessagingFactory _messagingFactory;
  
      private readonly NamespaceManager _namespaceManager;
  
      private readonly QueueClient _queueClient;
  
      private readonly ISettingsManager _settingsManager;
  
      public ServiceBusClient(ISettingsManager settingsManager, string queueName)
      {
          _settingsManager = settingsManager;
  
          var connectionString = _settingsManager.GetSetting&lt;string&gt;(SERVICE_BUS_CONNECTION_STRING_KEY);
  
          _namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
          _messagingFactory = MessagingFactory.CreateFromConnectionString(connectionString);
  
          _queueClient = GetOrCreateQueue(queueName);
      }
  
      public void Dispose()
      {
          _messagingFactory.Close();
      }
  
      public BrokeredMessage ReceiveTopMessage()
      {
          return _queueClient.Receive(TimeSpan.FromSeconds(DEFAULT_WAIT_TIME_IN_SECONDS));
      }
  
      public void SendMessage(object bodyObject)
      {
          var message = new BrokeredMessage(bodyObject);
  
          _queueClient.Send(message);
      }
  
      private QueueClient GetOrCreateQueue(string queueName)
      {
          var queue = !_namespaceManager.QueueExists(queueName)
                          ? _namespaceManager.CreateQueue(queueName)
                          : _namespaceManager.GetQueue(queueName);
  
          return _messagingFactory.CreateQueueClient(queue.Path, ReceiveMode.PeekLock);
      }
  
  }
  </code></pre>
  
  <p>As you can see I initialise the <code>NamespaceManager</code>, <code>MessagingFactory</code> and <code>QueueClient</code> inside the constructor: they are then reused when calling <code>SendMessage()</code> and <code>ReceiveTopMessage()</code> with the connection closed using a <code>Dispose()</code> method.</p>
  
  <p>My question is whether the approach I am using is <em>safe</em>; will keeping a single instance of the <code>QueueClient</code> open whilst a worker role enumerates through all the messages on the queue (A process which could keep the connection open for quite a while with a long wait between calls to <code>ReceiveTopMessage()</code>) work consistently without transient issues, or is it prudent to open and close the connection each time?</p>
  
  <p>As an aside; how is the transient fault handling conducted in the Microsoft Service Bus code?  Is it conducted by default or do we need to implement the <a href="http://code.msdn.microsoft.com/windowsazure/Transient-Fault-Handling-b209151f" rel="noreferrer">Transient Fault Handling Framework</a>?</p>