/// <summary>
        /// Connects the ServiceBusHelper object to service bus namespace contained in the ServiceBusNamespaces dictionary.
        /// </summary>
        /// <param name="uri">The full uri of the service namespace.</param>
        /// <param name="issuerName">The issuer name of the shared secret credentials.</param>
        /// <param name="issuerSecret">The issuer secret of the shared secret credentials.</param>
        /// <param name="transportType">The current transport type.</param>
        /// <param name="sharedAccessKeyName">The shared access key name.</param>
        /// <param name="sharedAccessKey">The shared access key.</param>
        /// <returns>True if the operation succeeds, false otherwise.</returns>
        public bool Connect(string uri, 
                            string issuerName, 
                            string issuerSecret,
                            string sharedAccessKeyName,
                            string sharedAccessKey,
                            TransportType transportType)
        {
            Func<bool> func = (() =>
            {
                if (string.IsNullOrWhiteSpace(uri))
                {
                    throw new ArgumentException(ServiceBusUriArgumentCannotBeNull);
                }
                if (string.IsNullOrWhiteSpace(issuerName))
                {
                    throw new ArgumentException(ServiceBusIssuerNameArgumentCannotBeNull);
                }
                if (string.IsNullOrWhiteSpace(issuerSecret))
                {
                    throw new ArgumentException(ServiceBusIssuerSecretArgumentCannotBeNull);
                }

                // Create the service URI using the uri specified in the Connect form
                namespaceUri = new Uri(uri);
                if (!string.IsNullOrWhiteSpace(namespaceUri.Host) &&
                    namespaceUri.Host.Contains('.'))
                {
                    Namespace = namespaceUri.Host.Substring(0, namespaceUri.Host.IndexOf('.'));
                }

                // Create the atom feed URI using the scheme, namespace and service name (optional)
                if (uri.Substring(0, 4) != Uri.UriSchemeHttp)
                {
                    var index = uri.IndexOf("://", StringComparison.Ordinal);
                    if (index > 0)
                    {
                        uri = Uri.UriSchemeHttp + uri.Substring(index);
                    }
                }
                atomFeedUri = new Uri(uri);

                ServicePath = string.Empty;

                // Create shared secret credentials to to authenticate with the Access Control service, 
                // and acquire an access token that proves to the Service Bus insfrastructure that the 
                // the Service Bus Explorer is authorized to access the entities in the specified namespace.
                tokenProvider = ServiceBus.TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerSecret);
                try
                {
                    notificationHubTokenProvider = Azure.NotificationHubs.TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerSecret);
                }
                catch (Exception)
                {
                    // ignored
                }
                
                currentIssuerName = issuerName;
                currentIssuerSecret = issuerSecret;
                currentSharedAccessKeyName = sharedAccessKeyName;
                currentSharedAccessKey = sharedAccessKey;
                currentTransportType = transportType;

                // Create and instance of the NamespaceManagerSettings which 
                // specifies service namespace client settings and metadata.
                var namespaceManagerSettings = new ServiceBus.NamespaceManagerSettings
                {
                    TokenProvider = tokenProvider,
                    OperationTimeout = TimeSpan.FromMinutes(5)
                };
                var notificationHubNamespaceManagerSettings = new Azure.NotificationHubs.NamespaceManagerSettings
                {
                    TokenProvider = notificationHubTokenProvider,
                    OperationTimeout = TimeSpan.FromMinutes(5)
                };

                // The NamespaceManager class can be used for managing entities, 
                // such as queues, topics, subscriptions, and rules, in your service namespace. 
                // You must provide service namespace address and access credentials in order 
                // to manage your service namespace.
                namespaceManager = new ServiceBus.NamespaceManager(namespaceUri, namespaceManagerSettings);
                try
                {
                    notificationHubNamespaceManager = new Azure.NotificationHubs.NamespaceManager(namespaceUri, notificationHubNamespaceManagerSettings);
                }
                catch (Exception)
                {
                    // ignored
                }
                WriteToLogIf(traceEnabled, string.Format(CultureInfo.CurrentCulture, ServiceBusIsConnected, namespaceUri.AbsoluteUri));

                // The MessagingFactorySettings specifies the service bus messaging factory settings.
                var messagingFactorySettings = new MessagingFactorySettings
                {
                    TokenProvider = tokenProvider,
                    OperationTimeout = TimeSpan.FromMinutes(5)
                };
                // In the first release of the service bus, the only available transport protocol is sb 
                if (scheme == DefaultScheme)
                {
                    messagingFactorySettings.NetMessagingTransportSettings = new NetMessagingTransportSettings();
                }

                // As the name suggests, the MessagingFactory class is a Factory class that allows to create
                // instances of the QueueClient, TopicClient and SubscriptionClient classes.
                MessagingFactory = MessagingFactory.Create(namespaceUri, messagingFactorySettings);
                WriteToLogIf(traceEnabled, MessageFactorySuccessfullyCreated);
                return true;
            });
            return RetryHelper.RetryFunc(func, writeToLog);
        }
 /// <summary>
 /// Initializes a new instance of the ServiceBusHelper class.
 /// </summary>
 /// <param name="writeToLog">WriteToLog method.</param>
 /// <param name="serviceBusHelper">Base ServiceBusHelper.</param>
 public ServiceBusHelper(WriteToLogDelegate writeToLog, ServiceBusHelper serviceBusHelper)
 {
     this.writeToLog = writeToLog;
     AtomFeedUri = serviceBusHelper.AtomFeedUri;
     MessageDeferProviderType = serviceBusHelper.MessageDeferProviderType;
     connectionString = serviceBusHelper.ConnectionString;
     namespaceManager = serviceBusHelper.NamespaceManager;
     notificationHubNamespaceManager = serviceBusHelper.NotificationHubNamespaceManager;
     MessagingFactory = serviceBusHelper.MessagingFactory;
     Namespace = serviceBusHelper.Namespace;
     NamespaceUri = serviceBusHelper.NamespaceUri;
     MessageDeferProviderType = serviceBusHelper.MessageDeferProviderType;
     Scheme = serviceBusHelper.Scheme;
     ServiceBusNamespaces = serviceBusHelper.ServiceBusNamespaces;
     BrokeredMessageInspectors = serviceBusHelper.BrokeredMessageInspectors;
     EventDataInspectors = serviceBusHelper.EventDataInspectors;
     BrokeredMessageGenerators = serviceBusHelper.BrokeredMessageGenerators;
     EventDataGenerators = serviceBusHelper.EventDataGenerators;
     ServicePath = serviceBusHelper.ServicePath;
     TokenProvider = serviceBusHelper.TokenProvider;
     notificationHubTokenProvider = serviceBusHelper.notificationHubTokenProvider;
     TraceEnabled = serviceBusHelper.TraceEnabled;
     IssuerName = serviceBusHelper.IssuerName;
     IssuerSecret = serviceBusHelper.IssuerSecret;
     SharedAccessKey = serviceBusHelper.SharedAccessKey;
     SharedAccessKeyName = serviceBusHelper.SharedAccessKeyName;
     TransportType = serviceBusHelper.TransportType;
 }