/// <summary> /// Get queue from queue identifier /// </summary> /// <param name="queueIdentifier">Queue identifier</param> /// <returns>Messaging queue</returns> public async Task <Queue> GetQueue(QueueIdentifier queueIdentifier) { QueueDescriptor queueDescriptor = QueueDescriptorProvider.Queues[queueIdentifier]; string serviceBusConnectionString = await this.connectionStringProvider.GetServiceBusConnectionString(queueDescriptor.ServiceBusInstanceType); string queueName = queueDescriptor.QueueName; string uniqueQueueIdentity = string.Join(":", serviceBusConnectionString, queueName); // cachedQueueObjects is a thread-safe dictionary (ConcurrentDictionary). If uniqueQueueIdentity is not present // in cachedStoreObjects, try adding it. Since GetQueue can be called concurrently by // different threads, it is possible for two (or more) threads to attempt inserting uniqueQueueIdentity // concurrently in the cachedQueueObjects. That's ok, because the call to TryAdd is guaranteed to be thread-safe. // One of the threads will not be able to insert (i.e., TryAdd will return false), but the code will happily execute // and fall through to the return statement. // This code makes no use of locking on the common path (i.e., reads of cachedStoreObjects). if (!cachedQueueObjects.ContainsKey(uniqueQueueIdentity)) { ServiceBusQueue serviceBusQueue = await ServiceBusQueue.Create(serviceBusConnectionString, queueName, this.batchIntervalMs); Queue queue = new Queue(serviceBusQueue); cachedQueueObjects.TryAdd(uniqueQueueIdentity, queue); } return(cachedQueueObjects[uniqueQueueIdentity]); }
/// <summary> /// Initialization routine /// </summary> /// <param name="action">action</param> /// <returns>initialization task</returns> private async Task Initialize(Action action) { // load the environment configuration file from UtilsInternal var sr = new FileSettingsReader(ConfigurationManager.AppSettings["ConfigRelativePath"] + Path.DirectorySeparatorChar + environmentName + ".config"); var certThumbprint = sr.ReadValue(SocialPlusCertThumbprint); var clientID = sr.ReadValue(EmbeddedSocialClientIdSetting); var storeLocation = StoreLocation.CurrentUser; var vaultUrl = sr.ReadValue(SocialPlusVaultUrlSetting); ICertificateHelper cert = new CertificateHelper(certThumbprint, clientID, storeLocation); IKeyVaultClient client = new AzureKeyVaultClient(cert); var log = new Log(LogDestination.Console, Log.DefaultCategoryName); var kv = new KV(log, clientID, vaultUrl, certThumbprint, storeLocation, client); var kvReader = new KVSettingsReader(sr, kv); IConnectionStringProvider connectionStringProvider = new ConnectionStringProvider(kvReader); int queueBatchIntervalMs = int.Parse(sr.ReadValue(ServiceBusBatchIntervalMsSetting)); QueueManager queueManager = new QueueManager(connectionStringProvider, queueBatchIntervalMs); var sbConnect = await connectionStringProvider.GetServiceBusConnectionString(ServiceBusInstanceType.Default); // the ListQueues action requires an instance of a ServiceBus object if (action == Action.ListQueues) { this.sb = new ServiceBus(sbConnect); } // all the remaining actions operate on an instance of a ServiceBusQueue object if (action != Action.ListQueues) { // ParseArgs() ensures that queueName is valid here this.sbQueue = await ServiceBusQueue.Create(sbConnect, queueName, queueBatchIntervalMs); this.selectedQueue = await queueManager.GetQueue((QueueIdentifier)selectedQueueId); } }