コード例 #1
0
        private async Task <QueueClient> InitializeQueueClient(string queueName, QueueClientOptions options)
        {
            var queueClient = new QueueClient(_settingService.GetStorageAccount(), queueName, options);
            await queueClient.CreateIfNotExistsAsync();

            return(queueClient);
        }
コード例 #2
0
        private QueueClient GetCloudQueueClient(AzureQueueOptions options, ILogger logger)
        {
            try
            {
                var clientOptions = new QueueClientOptions
                {
                    Retry =
                    {
                        Mode           = RetryMode.Fixed,
                        Delay          = AzureQueueDefaultPolicies.PauseBetweenQueueOperationRetries,
                        MaxRetries     = AzureQueueDefaultPolicies.MaxQueueOperationRetries,
                        NetworkTimeout = AzureQueueDefaultPolicies.QueueOperationTimeout,
                    }
                };

                var client = options.ServiceUri != null
                    ? new QueueClient(new Uri(options.ServiceUri, QueueName), options.TokenCredential, clientOptions)
                    : new QueueClient(options.ConnectionString, QueueName, clientOptions);
                return(client);
            }
            catch (Exception exc)
            {
                logger.Error((int)AzureQueueErrorCode.AzureQueue_14, String.Format("Error creating GetCloudQueueOperationsClient."), exc);
                throw;
            }
        }
コード例 #3
0
        // Helper method for creating queueClient
        async Task <QueueClient> CreateQueueClient()
        {
            if (String.IsNullOrWhiteSpace(_connectionString) || String.IsNullOrWhiteSpace(_queueName))
            {
                throw new NullReferenceException("ConnectionString or Queue name is null. Remember to call Init() method, providing a connection string and queue name");
            }

            var queueClientOptions = new QueueClientOptions()
            {
                MessageEncoding = QueueMessageEncoding.Base64
            };

            // Instantiate a QueueClient which will be used to create and manipulate the queue
            QueueClient queueClient = new QueueClient(_connectionString, _queueName, queueClientOptions);

            var logMessagePrefix = $"Ensuring queue '{queueClient.Name}' exists. ";

            if (await queueClient.ExistsAsync())
            {
                _logger.LogTrace(logMessagePrefix + "Allready exists");
            }
            else
            {
                _logger.LogTrace(logMessagePrefix + "Did not exsist. Will create it");
            }

            // Create the queue if it doesn't already exist
            await queueClient.CreateIfNotExistsAsync();

            return(queueClient);
        }
コード例 #4
0
 private QueueServiceClient GetServiceClient_SharedKey_UnInstrumented(QueueClientOptions options = default)
 => new QueueServiceClient(
     new Uri(TestConfigDefault.QueueServiceEndpoint),
     new StorageSharedKeyCredential(
         TestConfigDefault.AccountName,
         TestConfigDefault.AccountKey),
     options ?? GetOptions());
コード例 #5
0
        private static QueueClient CreateQueueClient(string queueName)
        {
            var options = new QueueClientOptions
            {
                MessageEncoding = QueueMessageEncoding.Base64
            };

            return(new QueueClient(Constants.StorageConnectionStringSetting, queueName, options));
        }
コード例 #6
0
        private QueueClient GetSecondaryReadQueueClient(TenantConfiguration config, int numberOfReadFailuresToSimulate, out TestExceptionPolicy testExceptionPolicy, bool simulate404 = false, List <RequestMethod> enabledRequestMethods = null)
        {
            QueueClientOptions options = getSecondaryStorageOptions(config, out testExceptionPolicy, numberOfReadFailuresToSimulate, simulate404, enabledRequestMethods);

            return(InstrumentClient(
                       new QueueClient(
                           new Uri(config.QueueServiceEndpoint).AppendToPath(GetNewQueueName()),
                           new StorageSharedKeyCredential(config.AccountName, config.AccountKey),
                           options)));
        }
コード例 #7
0
        private QueueClientOptions getSecondaryStorageOptions(TenantConfiguration config, out TestExceptionPolicy testExceptionPolicy, int numberOfReadFailuresToSimulate = 1, bool simulate404 = false, List <RequestMethod> enabledRequestMethods = null)
        {
            QueueClientOptions options = GetOptions();

            options.GeoRedundantSecondaryUri = new Uri(config.QueueServiceSecondaryEndpoint);
            options.Retry.MaxRetries         = 4;
            testExceptionPolicy = new TestExceptionPolicy(numberOfReadFailuresToSimulate, options.GeoRedundantSecondaryUri, simulate404, enabledRequestMethods);
            options.AddPolicy(testExceptionPolicy, HttpPipelinePosition.PerRetry);
            return(options);
        }
コード例 #8
0
        public async Task NotifyOnUploadAsync(string message)
        {
            var options = new QueueClientOptions()
            {
                MessageEncoding = QueueMessageEncoding.Base64
            };
            var queueClient = new QueueClient(_configuration[Defines.STORAGE_ACCOUNT_CONNECTION_STRING_SECTTION], _configuration[Defines.QUEUE_NAME_SECTION], options);

            queueClient.CreateIfNotExists();
            var encodedMessage = Convert.ToBase64String(Encoding.UTF8.GetBytes(message));
            await queueClient.SendMessageAsync(encodedMessage);
        }
コード例 #9
0
        public QueueServiceClient GetQueueServiceClient(QueueClientOptions queueClientOptions = default)
        {
            if (queueClientOptions == default)
            {
                queueClientOptions = new QueueClientOptions()
                {
                    MessageEncoding = QueueMessageEncoding.Base64
                };
            }

            queueClientOptions.Transport = GetTransport();
            return new QueueServiceClient(account.ConnectionString, queueClientOptions);
        }
コード例 #10
0
        public static QueueServiceClient GetQueueServiceClient(this AzuriteFixture azuriteFixture, QueueClientOptions queueClientOptions = default)
        {
            if (queueClientOptions == default)
            {
                queueClientOptions = new QueueClientOptions()
                {
                    MessageEncoding = QueueMessageEncoding.Base64
                };
            }

            queueClientOptions.Transport = azuriteFixture.GetTransport();
            return(new QueueServiceClient(azuriteFixture.GetAzureAccount().ConnectionString, queueClientOptions));
        }
コード例 #11
0
        private QueueClient CreateQueueClient(string queueName)
        {
            var options = new QueueClientOptions
            {
                MessageEncoding = QueueMessageEncoding.Base64
            };

            var queueClient = new QueueClient(_configuration["AppSettings:Storage"], queueName, options);

            queueClient.CreateIfNotExists();

            return(queueClient);
        }
コード例 #12
0
    public async Task <QueueServiceClient> GetQueueClientService(StorageType storageType)
    {
        var accountId = _storage.GetPrimaryAccount(storageType);

        _log.Verbose($"getting blob container (account_id: {accountId})");
        var(name, key) = await _storage.GetStorageAccountNameAndKey(accountId);

        var accountUrl = new Uri($"https://{name}.queue.core.windows.net");
        var options    = new QueueClientOptions {
            MessageEncoding = QueueMessageEncoding.Base64
        };

        return(new QueueServiceClient(accountUrl, new StorageSharedKeyCredential(name, key), options));
    }
コード例 #13
0
ファイル: Queue.cs プロジェクト: ranweiler/onefuzz
    public async Task <QueueServiceClient> GetQueueClientService(StorageType storageType)
    {
        var accountId = _storage.GetPrimaryAccount(storageType);

        _log.Verbose($"getting blob container (account_id: {accountId})");
        var(name, key) = await _storage.GetStorageAccountNameAndKey(accountId);

        var endpoint = _storage.GetQueueEndpoint(name);
        var options  = new QueueClientOptions {
            MessageEncoding = QueueMessageEncoding.Base64
        };

        return(new QueueServiceClient(endpoint, new StorageSharedKeyCredential(name, key), options));
    }
コード例 #14
0
        public void ConfigureMessageEncodingAsync()
        {
            var connectionString = ConnectionString;
            var queueName        = "foo";

            #region Snippet:Azure_Storage_Queues_Samples_Sample03_MessageEncoding_ConfigureMessageEncodingAsync

            QueueClientOptions queueClientOptions = new QueueClientOptions()
            {
                MessageEncoding = QueueMessageEncoding.Base64
            };

            QueueClient queueClient = new QueueClient(connectionString, queueName, queueClientOptions);
            #endregion
        }
コード例 #15
0
            public TestFixture(WebJobsTestEnvironment testEnvironment)
            {
                IHost host = new HostBuilder()
                             .ConfigureDefaultTestHost <TestFixture>(b =>
                {
                    b.AddAzureStorageBlobs().AddAzureStorageQueues();
                })
                             .Build();

                var queueOptions = new QueueClientOptions()
                {
                    MessageEncoding = QueueMessageEncoding.Base64
                };

                this.QueueServiceClient = new QueueServiceClient(testEnvironment.PrimaryStorageAccountConnectionString, queueOptions);
                this.BlobServiceClient  = new BlobServiceClient(testEnvironment.PrimaryStorageAccountConnectionString);
            }
コード例 #16
0
        public async Task Publish <T>(
            T @event,
            int retries      = 3,
            int retryMs      = 1000,
            bool exponential = true,
            TimeSpan?ttl     = null,
            TimeSpan?delay   = null) where T : PublishedEvent
        {
            if (delay != null && delay.Value.TotalMinutes > 7 * 24 * 60)
            {
                throw new Exception("delay can't be greater than 7 days");
            }

            if (delay != null && ttl != null && delay.Value.TotalMilliseconds > ttl.Value.TotalMilliseconds)
            {
                throw new Exception("delay can't be greater than the ttl");
            }

            var queueName = GetQueueNameFromEvent(typeof(T));

            var opts = new QueueClientOptions(QueueClientOptions.ServiceVersion.V2020_10_02);

            if (retries > 0)
            {
                opts.Retry.Delay      = TimeSpan.FromMilliseconds(retryMs);
                opts.Retry.Mode       = exponential ? Azure.Core.RetryMode.Exponential : Azure.Core.RetryMode.Fixed;
                opts.Retry.MaxRetries = retries;
            }

            var queue = new QueueClient(_connectionString, queueName, opts);

            if (!InitializedQueues.Contains(queueName))
            {
                await queue.CreateIfNotExistsAsync();

                InitializedQueues.Add(queueName);
            }

            var msg = await _objectSerializer.Serialize(@event);

            await queue.SendMessageAsync(
                msg,
                visibilityTimeout : delay,
                timeToLive : ttl
                );
        }
コード例 #17
0
        public static void Run([QueueTrigger("hr-main-new-image-metadata", Connection = "HR_IMAGE_QUEUE_CX")] string myQueueItem, ILogger log)
        {
            if (!String.IsNullOrEmpty(myQueueItem))
            {
                //1- Get env variable
                String queueStorageCx = Environment.GetEnvironmentVariable(_QUEUE_STORAGE_CONNEXION_KEY);
                String queueNewImageMetaDataForUserAgents = Environment.GetEnvironmentVariable(_QUEUE_FOR_USER_AGENTS_NAME_KEY);


                //2- Log received info
                var    base64EncodedBytes = Convert.FromBase64String(myQueueItem);
                String convertedString    = Encoding.UTF8.GetString(base64EncodedBytes);
                log.LogInformation($"C# Queue trigger function processed original value: {myQueueItem}");
                log.LogInformation($"C# Queue trigger function processed converted value: {convertedString}");

                //3- get or create queueFor user agents
                // Get the connection string from app settings
                // Instantiate a QueueClient which will be used to create and manipulate the queue
                QueueClientOptions queueClientOptions = new QueueClientOptions()
                {
                    MessageEncoding = QueueMessageEncoding.Base64
                };

                QueueClient queueClient = new QueueClient(
                    queueStorageCx,
                    queueNewImageMetaDataForUserAgents,
                    queueClientOptions);
                queueClient.CreateIfNotExists();

                //4- Send message
                if (queueClient.Exists())
                {
                    var queueResponse = queueClient.SendMessage(myQueueItem);
                }
                else
                {
                    log.LogInformation($"Error when creating queue");
                }
            }
            else
            {
                log.LogInformation($"Empty entry");
            }
        }
コード例 #18
0
        public static QueueClient getQueueClient(string storName, string queueName)
        {
            ManagedIdentityCredential managedIdentityCredential = new ManagedIdentityCredential();

            QueueClientOptions queueOptions = new QueueClientOptions
            {
                Retry =
                {
                    MaxRetries = 5,
                    Mode       = 0
                },
                MessageEncoding = QueueMessageEncoding.Base64
            };

            string      queueEndpoint = string.Format("https://{0}.queue.core.windows.net/{1}", storName, queueName);
            QueueClient queueClient   = new QueueClient(new Uri(queueEndpoint), managedIdentityCredential, queueOptions);

            return(queueClient);
        }
コード例 #19
0
        public static IServiceCollection AddQueueClient(this IServiceCollection services, IConfiguration configuration)
        {
            var positionOptions = new MessageQueueOptions();

            configuration.GetSection(MessageQueueOptions.SectionName).Bind(positionOptions);

            positionOptions.ConnectionString.ThrowIfNullOrEmpty(nameof(MessageQueueOptions.ConnectionString));
            positionOptions.QueueName.ThrowIfNullOrEmpty(nameof(MessageQueueOptions.QueueName));

            services.AddTransient <IQueueService, StorageQueueService>();
            services.AddTransient <QueueClient>(x => {
                var options = new QueueClientOptions
                {
                    MessageEncoding = QueueMessageEncoding.Base64
                };

                return(new QueueClient(positionOptions.ConnectionString, positionOptions.QueueName, options));
            });

            return(services);
        }
コード例 #20
0
        public QueueClientOptions GetOptions()
        {
            var options = new QueueClientOptions
            {
                Diagnostics = { IsLoggingEnabled = true },
                Retry       =
                {
                    Mode       = RetryMode.Exponential,
                    MaxRetries = Constants.MaxReliabilityRetries,
                    Delay      = TimeSpan.FromSeconds(Mode == RecordedTestMode.Playback ? 0.01 : 1),
                    MaxDelay   = TimeSpan.FromSeconds(Mode == RecordedTestMode.Playback ? 0.1 : 60)
                },
            };

            if (Mode != RecordedTestMode.Live)
            {
                options.AddPolicy(new RecordedClientRequestIdPolicy(Recording), HttpPipelinePosition.PerCall);
            }

            return(InstrumentClientOptions(options));
        }
コード例 #21
0
        public void MessageDecodingFailedHandlerAsync()
        {
            var connectionString = ConnectionString;
            var queueName        = "foo";

            #region Snippet:Azure_Storage_Queues_Samples_Sample03_MessageEncoding_MessageDecodingFailedHandlerAsync

            QueueClientOptions queueClientOptions = new QueueClientOptions()
            {
                MessageEncoding = QueueMessageEncoding.Base64
            };

            queueClientOptions.MessageDecodingFailed += async(QueueMessageDecodingFailedEventArgs args) =>
            {
                if (args.PeekedMessage != null)
                {
                    Console.WriteLine($"Invalid message has been peeked, message id={args.PeekedMessage.MessageId} body={args.PeekedMessage.Body}");
                }
                else if (args.ReceivedMessage != null)
                {
                    Console.WriteLine($"Invalid message has been received, message id={args.ReceivedMessage.MessageId} body={args.ReceivedMessage.Body}");

                    if (args.RunSynchronously)
                    {
                        args.Queue.DeleteMessage(args.ReceivedMessage.MessageId, args.ReceivedMessage.PopReceipt);
                    }
                    else
                    {
                        await args.Queue.DeleteMessageAsync(args.ReceivedMessage.MessageId, args.ReceivedMessage.PopReceipt);
                    }
                }
            };

            QueueClient queueClient = new QueueClient(connectionString, queueName, queueClientOptions);
            #endregion
        }
コード例 #22
0
        /// <summary>
        /// Add message in main queue to start thumbnail processing
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public async Task OnNewImageAsync(HRSubmitPictureListItemDto message)
        {
            if (message == null)
            {
                return;
            }
            // Get the connection string from app settings
            // Instantiate a QueueClient which will be used to create and manipulate the queue
            QueueClientOptions queueClientOptions = new QueueClientOptions()
            {
                MessageEncoding = QueueMessageEncoding.Base64
            };

            QueueClient queueClient = new QueueClient(
                _config.Value?.HRSubmittedPictureCx,
                _config.Value?.HRMainNewImageQueueName,
                queueClientOptions);

            // Create the queue if it doesn't already exist
            queueClient.CreateIfNotExists();

            if (queueClient.Exists())
            {
                String messageToString = JsonConvert.SerializeObject(message);

                var    plainTextBytes = System.Text.Encoding.UTF8.GetBytes(messageToString);
                string message64      = System.Convert.ToBase64String(plainTextBytes);
                using var queueTask = queueClient.SendMessageAsync(message64);

                await queueTask;
                if (!queueTask.IsCompletedSuccessfully)
                {
                    throw new Exception("queueClient.SendMessageAsync failure.");
                }
            }
        }
コード例 #23
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EncryptedQueueClient"/>
 /// class.
 /// </summary>
 /// <param name="connectionString">
 /// A connection string includes the authentication information
 /// required for your application to access data in an Azure Storage
 /// account at runtime.
 ///
 /// For more information, <see href="https://docs.microsoft.com/en-us/azure/storage/common/storage-configure-connection-string"/>.
 /// </param>
 /// <param name="queueName">
 /// The name of the encrypted queue in the storage account to reference.
 /// </param>
 /// <param name="options">
 /// Optional client options that define the transport pipeline
 /// policies for authentication, retries, etc., that are applied to
 /// every request.
 /// </param>
 public EncryptedQueueClient(string connectionString, string queueName, QueueClientOptions options)
     : base(connectionString, queueName, options)
 {
 }
コード例 #24
0
 public QueueServiceClient GetServiceClient_SharedKey(QueueClientOptions options = default)
 => InstrumentClient(GetServiceClient_SharedKey_UnInstrumented(options));
コード例 #25
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EncryptedQueueClient"/>
 /// class.
 /// </summary>
 /// <param name="queueUri">
 /// A <see cref="Uri"/> referencing the encrypted queue that includes the
 /// name of the account, and the name of the queue.
 /// </param>
 /// <param name="credential">
 /// The shared key credential used to sign requests.
 /// </param>
 /// <param name="options">
 /// Optional client options that define the transport pipeline
 /// policies for authentication, retries, etc., that are applied to
 /// every request.
 /// </param>
 public EncryptedQueueClient(Uri queueUri, StorageSharedKeyCredential credential, QueueClientOptions options = default)
     : base(queueUri, credential, options)
 {
 }
コード例 #26
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EncryptedQueueClient"/>
 /// class.
 /// </summary>
 /// <param name="queueUri">
 /// A <see cref="Uri"/> referencing the encrypted queue that includes the
 /// name of the account, and the name of the queue.
 /// </param>
 /// <param name="credential">
 /// The token credential used to sign requests.
 /// </param>
 /// <param name="options">
 /// Optional client options that define the transport pipeline
 /// policies for authentication, retries, etc., that are applied to
 /// every request.
 /// </param>
 public EncryptedQueueClient(Uri queueUri, TokenCredential credential, QueueClientOptions options = default)
     : base(queueUri, credential, options)
 {
 }
コード例 #27
0
        // PRIVATE//

        private async Task InitializeStorage()
        {
            //TODO: #45 Not thread safe
            if (_isStorageInitialized)
            {
                return;
            }

            var error = "";

            try
            {
                //TODO: #45 Swallowing errors
                if (!string.IsNullOrEmpty(_settingService.GetStorageAccount()))
                {
                    // Queues Initialization

                    // Retry policy appropriate for a web user interface.
                    var queueClientOptions = new QueueClientOptions();
                    queueClientOptions.Retry.Mode       = RetryMode.Fixed;
                    queueClientOptions.Retry.Delay      = TimeSpan.FromSeconds(3);
                    queueClientOptions.Retry.MaxRetries = 3;


                    var tripManagersQueueName = _settingService.GetTripManagersQueueName();
                    if (!string.IsNullOrEmpty(tripManagersQueueName))
                    {
                        _tripManagersQueue = await InitializeQueueClient(tripManagersQueueName, queueClientOptions);
                    }
                    else
                    {
                        //TODO: #45 Swallowing errors
                        _loggerService.Log("tripManagersQueueName is empty");
                    }

                    var tripMonitorsQueueName = _settingService.GetTripMonitorsQueueName();
                    if (!string.IsNullOrEmpty(tripMonitorsQueueName))
                    {
                        _tripMonitorsQueue = await InitializeQueueClient(tripMonitorsQueueName, queueClientOptions);
                    }
                    else
                    {
                        _loggerService.Log("tripMonitorsQueueName is empty");
                    }

                    var tripDemosQueueName = _settingService.GetTripDemosQueueName();
                    if (!string.IsNullOrEmpty(tripDemosQueueName))
                    {
                        _tripDemosQueue = await InitializeQueueClient(tripDemosQueueName, queueClientOptions);
                    }
                    else
                    {
                        _loggerService.Log("tripDemosQueueName is empty");
                    }

                    var tripDriversQueueName = _settingService.GetTripDriversQueueName();
                    if (!string.IsNullOrEmpty(tripDriversQueueName))
                    {
                        _tripDriversQueue = await InitializeQueueClient(tripDriversQueueName, queueClientOptions);
                    }
                    else
                    {
                        _loggerService.Log("tripDriversQueueName is empty");
                    }

                    _isStorageInitialized = true;
                }
            }
            catch (Exception ex)
            {
                error = $"Error while initializing storage: {ex.Message}";
                throw new Exception(error);
            }
            finally
            {
                _loggerService.Log($"{LOG_TAG} - InitializeStorage - Error: {error}");
            }
        }
コード例 #28
0
 protected override QueueServiceClient CreateClientFromTokenCredential(Uri endpointUri, TokenCredential tokenCredential, QueueClientOptions options)
 {
     return(_queueServiceClient);
 }
コード例 #29
0
 protected override QueueServiceClient CreateClientFromConnectionString(string connectionString, QueueClientOptions options)
 {
     return(_queueServiceClient);
 }
コード例 #30
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EncryptedQueueClient"/>
 /// class.
 /// </summary>
 /// <param name="queueUri">
 /// A <see cref="Uri"/> referencing the encrypted queue that includes the
 /// name of the account, and the name of the queue.
 /// </param>
 /// <param name="options">
 /// Optional client options that define the transport pipeline
 /// policies for authentication, retries, etc., that are applied to
 /// every request.
 /// </param>
 public EncryptedQueueClient(Uri queueUri, QueueClientOptions options = default)
     : base(queueUri, options)
 {
 }