コード例 #1
0
        //---------------------------------------------------
        // Use Custom Request ID
        //---------------------------------------------------

        public void UseCustomRequestID()
        {
            string HOSTNAME = "";
            string APPNAME  = "";
            string USERID   = "";

            // <Snippet_UseCustomRequestID>

            var connectionString = Constants.connectionString;

            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

            BlobContainerClient blobContainerClient = blobServiceClient.GetBlobContainerClient("demcontainer");

            BlobClient blobClient = blobContainerClient.GetBlobClient("testImage.jpg");

            string clientRequestID = String.Format("{0} {1} {2} {3}", HOSTNAME, APPNAME, USERID, Guid.NewGuid().ToString());

            using (HttpPipeline.CreateClientRequestIdScope(clientRequestID))
            {
                BlobDownloadInfo download = blobClient.Download();

                using (FileStream downloadFileStream = File.OpenWrite("C:\\testImage.jpg"))
                {
                    download.Content.CopyTo(downloadFileStream);
                    downloadFileStream.Close();
                }
            }

            // </Snippet_UseCustomRequestID>
        }
コード例 #2
0
        public async Task ReadsRequestIdValueOfScope()
        {
            var transport = new MockTransport(r => new MockResponse(200));

            using (HttpPipeline.CreateClientRequestIdScope("custom-id"))
            {
                await SendGetRequest(transport, ReadClientRequestIdPolicy.Shared);
            }

            Assert.AreEqual(transport.SingleRequest.ClientRequestId, "custom-id");
        }
コード例 #3
0
        public async Task CanResetRequestIdValueOfParentScope()
        {
            var transport = new MockTransport(r => new MockResponse(200));

            using (HttpPipeline.CreateClientRequestIdScope("custom-id"))
                using (HttpPipeline.CreateClientRequestIdScope(null))
                {
                    await SendGetRequest(transport, ReadClientRequestIdPolicy.Shared);
                }

            Assert.IsNotEmpty(transport.SingleRequest.ClientRequestId);
        }
コード例 #4
0
        public void ClientRequestId()
        {
            #region Snippet:ClientRequestId
            var secretClient = new SecretClient(new Uri("http://example.com"), new DefaultAzureCredential());

            using (HttpPipeline.CreateClientRequestIdScope("<custom-client-request-id>"))
            {
                // The HTTP request resulting from the client call would have x-ms-client-request-id value set to <custom-client-request-id>
                secretClient.GetSecret("<secret-name>");
            }
            #endregion
        }
コード例 #5
0
        public async Task <TaskSeriesCommandResult> ExecuteAsync(CancellationToken cancellationToken)
        {
            lock (_stopWaitingTaskSourceLock)
            {
                if (_stopWaitingTaskSource != null)
                {
                    _stopWaitingTaskSource.TrySetResult(null);
                }

                _stopWaitingTaskSource = new TaskCompletionSource <object>(TaskCreationOptions.RunContinuationsAsynchronously);
            }

            QueueMessage[] batch           = null;
            string         clientRequestId = Guid.NewGuid().ToString();
            Stopwatch      sw = null;

            using (HttpPipeline.CreateClientRequestIdScope(clientRequestId))
            {
                try
                {
                    if (!_queueExists.HasValue || !_queueExists.Value)
                    {
                        // Before querying the queue, determine if it exists. This
                        // avoids generating unecessary exceptions (which pollute AppInsights logs)
                        // Once we establish the queue exists, we won't do the existence
                        // check anymore (steady state).
                        // However the queue can always be deleted from underneath us, in which case
                        // we need to recheck. That is handled below.
                        _queueExists = await _queue.ExistsAsync(cancellationToken).ConfigureAwait(false);
                    }

                    if (_queueExists.Value)
                    {
                        sw = Stopwatch.StartNew();

                        Response <QueueMessage[]> response = await _queue.ReceiveMessagesAsync(_queueProcessor.QueuesOptions.BatchSize, _visibilityTimeout, cancellationToken).ConfigureAwait(false);

                        batch = response.Value;

                        int count = batch?.Length ?? -1;
                        Logger.GetMessages(_logger, _functionDescriptor.LogName, _queue.Name, response.GetRawResponse().ClientRequestId, count, sw.ElapsedMilliseconds);
                    }
                }
                catch (RequestFailedException exception)
                {
                    // if we get ANY errors querying the queue reset our existence check
                    // doing this on all errors rather than trying to special case not
                    // found, because correctness is the most important thing here
                    _queueExists = null;

                    if (exception.IsNotFoundQueueNotFound() ||
                        exception.IsConflictQueueBeingDeletedOrDisabled() ||
                        exception.IsServerSideError())
                    {
                        long pollLatency = sw?.ElapsedMilliseconds ?? -1;
                        Logger.HandlingStorageException(_logger, _functionDescriptor.LogName, _queue.Name, clientRequestId, pollLatency, exception);

                        // Back off when no message is available, or when
                        // transient errors occur
                        return(CreateBackoffResult());
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            if (batch == null)
            {
                return(CreateBackoffResult());
            }

            bool foundMessage = false;

            foreach (var message in batch)
            {
                if (message == null)
                {
                    continue;
                }

                foundMessage = true;

                // Note: Capturing the cancellationToken passed here on a task that continues to run is a slight abuse
                // of the cancellation token contract. However, the timer implementation would not dispose of the
                // cancellation token source until it has stopped and perhaps also disposed, and we wait for all
                // outstanding tasks to complete before stopping the timer.
                Task task = ProcessMessageAsync(message, _visibilityTimeout, cancellationToken);

                // Having both WaitForNewBatchThreshold and this method mutate _processing is safe because the timer
                // contract is serial: it only calls ExecuteAsync once the wait expires (and the wait won't expire until
                // WaitForNewBatchThreshold has finished mutating _processing).
                _processing.Add(task);
            }

            // Back off when no message was found.
            if (!foundMessage)
            {
                return(CreateBackoffResult());
            }

            _foundMessageSinceLastDelay = true;
            return(CreateSucceededResult());
        }