public BlobClientOptions GetOptions(bool parallelRange = false)
        {
            var options = new BlobClientOptions(_serviceVersion)
            {
                Diagnostics = { IsLoggingEnabled = true },
                Retry       =
                {
                    Mode           = RetryMode.Exponential,
                    MaxRetries     = Storage.Constants.MaxReliabilityRetries,
                    Delay          = TimeSpan.FromSeconds(Mode == RecordedTestMode.Playback ? 0.01 : 1),
                    MaxDelay       = TimeSpan.FromSeconds(Mode == RecordedTestMode.Playback ? 0.1 : 60),
                    NetworkTimeout = TimeSpan.FromSeconds(Mode == RecordedTestMode.Playback ? 100 : 400),
                },
            };

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

            return(InstrumentClientOptions(options));
        }
        public BlobClientOptions GetOptions()
        {
            var options = new BlobClientOptions
            {
                Diagnostics = { IsLoggingEnabled = true },
                Retry       =
                {
                    Mode       = RetryMode.Exponential,
                    MaxRetries = Constants.MaxReliabilityRetries,
                    Delay      = TimeSpan.FromSeconds(Mode == RecordedTestMode.Playback ? 0.01 : 0.5),
                    MaxDelay   = TimeSpan.FromSeconds(Mode == RecordedTestMode.Playback ? 0.1 : 10)
                },
                Transport = GetTransport()
            };

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

            return(InstrumentClientOptions(options));
        }
Exemple #3
0
        static async Task Main(string[] args)
        {
            var connectionString = Environment.GetEnvironmentVariable("STORAGE_CONNECTION_STRING");

            var blobClientOptions = new BlobClientOptions();

            // When using an HTTPS _faultInjectorUri, you must either trust the .NET developer certificate,
            // or uncomment the following line to disable SSL validation.
            // blobClientOptions.Transport = new HttpClientTransport(new System.Net.Http.HttpClient(new System.Net.Http.HttpClientHandler()
            // {
            //     ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true
            // }));

            // FaultInjectionPolicy should be per-retry to run as late as possible in the pipeline.  For example, some
            // clients compute a request signature as a per-retry policy, and FaultInjectionPolicy should run after the
            // signature is computed to avoid altering the signature.
            blobClientOptions.AddPolicy(new FaultInjectionPolicy(_faultInjectorUri), HttpPipelinePosition.PerRetry);

            // The request can be redirected to the HttpFaultInjector using either a Transport or a Policy.
            // A Policy is recommended by default.
            // blobClientOptions.Transport = new FaultInjectionTransport(blobClientOptions.Transport, _faultInjectorUri);

            // Use a single fast retry instead of the default settings
            blobClientOptions.Retry.Mode       = RetryMode.Fixed;
            blobClientOptions.Retry.Delay      = TimeSpan.Zero;
            blobClientOptions.Retry.MaxRetries = 1;

            var blobClient = new BlobClient(connectionString, "sample", "sample.txt", blobClientOptions);

            Console.WriteLine("Sending request...");
            var response = await blobClient.DownloadAsync();

            var content = (new StreamReader(response.Value.Content)).ReadToEnd();

            Console.WriteLine($"Content: {content}");
        }
Exemple #4
0
 public static void AddPolicy(this BlobClientOptions options, HttpPipelinePolicy policy, HttpPipelinePosition position)
 {
     options.AddPolicy(position, policy);
 }
        public async Task ProcessEvents()
        {
            #region Snippet:EventHubs_Processor_Sample06_ChooseStorageVersion

            var storageConnectionString = "<< CONNECTION STRING FOR THE STORAGE ACCOUNT >>";
            var blobContainerName       = "<< NAME OF THE BLOB CONTAINER >>";
            /*@@*/
            /*@@*/ storageConnectionString = StorageTestEnvironment.Instance.StorageConnectionString;
            /*@@*/ blobContainerName       = _storageScope.ContainerName;

            var eventHubsConnectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>";
            var eventHubName  = "<< NAME OF THE EVENT HUB >>";
            var consumerGroup = "<< NAME OF THE EVENT HUB CONSUMER GROUP >>";
            /*@@*/
            /*@@*/ eventHubsConnectionString = EventHubsTestEnvironment.Instance.EventHubsConnectionString;
            /*@@*/ eventHubName  = _eventHubScope.EventHubName;
            /*@@*/ consumerGroup = _eventHubScope.ConsumerGroups.First();

            var storageClientOptions = new BlobClientOptions();

            storageClientOptions.AddPolicy(
                new StorageApiVersionPolicy(),
                HttpPipelinePosition.PerCall);

            var storageClient = new BlobContainerClient(
                storageConnectionString,
                blobContainerName,
                storageClientOptions);

            var processor = new EventProcessorClient(
                storageClient,
                consumerGroup,
                eventHubsConnectionString,
                eventHubName);

            try
            {
                using var cancellationSource = new CancellationTokenSource();
                cancellationSource.CancelAfter(TimeSpan.FromSeconds(30));

                // The event handlers are not relevant for this sample; for
                // illustration, they're delegating the implementation to the
                // host application.

                processor.ProcessEventAsync += Application.ProcessorEventHandler;
                processor.ProcessErrorAsync += Application.ProcessorErrorHandler;

                try
                {
                    await processor.StartProcessingAsync(cancellationSource.Token);

                    await Task.Delay(Timeout.Infinite, cancellationSource.Token);
                }
                catch (TaskCanceledException)
                {
                    // This is expected if the cancellation token is
                    // signaled.
                }
                finally
                {
                    // This may take up to the length of time defined
                    // as part of the configured TryTimeout of the processor;
                    // by default, this is 60 seconds.

                    await processor.StopProcessingAsync();
                }
            }
            catch
            {
                // The processor will automatically attempt to recover from any
                // failures, either transient or fatal, and continue processing.
                // Errors in the processor's operation will be surfaced through
                // its error handler.
                //
                // If this block is invoked, then something external to the
                // processor was the source of the exception.
            }
            finally
            {
                // It is encouraged that you unregister your handlers when you have
                // finished using the Event Processor to ensure proper cleanup.  This
                // is especially important when using lambda expressions or handlers
                // in any form that may contain closure scopes or hold other references.

                processor.ProcessEventAsync -= Application.ProcessorEventHandler;
                processor.ProcessErrorAsync -= Application.ProcessorErrorHandler;
            }

            #endregion
        }
Exemple #6
0
        static async Task Main(string[] args)
        {
            // Listen to events produced from all Azure SDK clients
            using AzureEventSourceListener listener = AzureEventSourceListener.CreateConsoleLogger();

            // We don't want to use connection string, since it is highly insecure
            //string connectionString = Environment.GetEnvironmentVariable("ITPRODEV_STORAGE_ACCOUNT_CONNECTION_STRING");
            //BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

            // We can configure and extend the HTTP pipeline through XxxClientOptions
            BlobClientOptions options = new BlobClientOptions();

            options.Retry.MaxRetries = 10;
            options.Diagnostics.IsTelemetryEnabled = false;
            options.AddPolicy(new MyCustomPolicy(), HttpPipelinePosition.PerCall);

            // Get a reference to our storage account. We use the public Uri and DefaultAzureCredential for authentication
            Uri blobServiceUri = new Uri("https://itprodev2020storage.blob.core.windows.net/");
            BlobServiceClient blobServiceClient = new BlobServiceClient(blobServiceUri, new DefaultAzureCredential(), options);

            // Get a reference to "itprodev-container" container. This clients inherits the HTTP Pipeline options from BlobServiceClient
            BlobContainerClient blobContainerClient = blobServiceClient.GetBlobContainerClient("itprodev-container");

            // Create container if not exists
            await blobContainerClient.CreateIfNotExistsAsync();

            // Create a dummy file
            string path = Utilities.CreateTempFile();

            // Get a reference to a blob
            BlobClient blob = blobContainerClient.GetBlobClient(Utilities.RandomName());

            // Upload file to blob
            await blob.UploadAsync(path);

            // Delete local file
            Utilities.DeleteFile(path);

            // asynchronously iterate through all blobs
            await foreach (BlobItem item in blobContainerClient.GetBlobsAsync())
            {
                // Get a path on disk where we can download the file
                string downloadPath = $"{item.Name}.txt";

                // Get a reference to blob and download its contents
                BlobDownloadInfo download = await blobContainerClient.GetBlobClient(item.Name).DownloadAsync();

                // Copy its contents to disk
                using FileStream file = File.OpenWrite(downloadPath);
                await download.Content.CopyToAsync(file);
            }

            // When a service call fails Azure.RequestFailedException would get thrown.
            // The exception type provides a Status property with an HTTP status code an an ErrorCode property with a service-specific error code.
            try
            {
                //...
            }
            // handle exception with status code 404
            catch (RequestFailedException e) when(e.Status == 404)
            {
                Console.WriteLine("ErrorCode " + e.ErrorCode);
            }
        }
        /// <summary>
        ///   Runs the sample using the specified Event Hubs and Azure storage connection information.
        /// </summary>
        ///
        /// <param name="eventHubsConnectionString">The connection string for the Event Hubs namespace that the sample should target.</param>
        /// <param name="eventHubName">The name of the Event Hub, sometimes known as its path, that the sample should run against.</param>
        /// <param name="blobStorageConnectionString">The connection string for the storage account where checkpoints and state should be persisted.</param>
        /// <param name="blobContainerName">The name of the blob storage container where checkpoints and state should be persisted.</param>
        ///
        public async Task RunAsync(string eventHubsConnectionString,
                                   string eventHubName,
                                   string blobStorageConnectionString,
                                   string blobContainerName)
        {
            // Sometimes the environment you are targeting uses a service version that's different from the one set by default by the
            // SDK.  The SDK targets the Azure cloud by default, and you may need to manually specify a different version in case you
            // are trying to run in another platform.
            //
            // For instance, this will happen when you try to run the Event Processor Client on an Azure Stack Hub version 2002.  The
            // processor makes use of the Azure Storage SDK, which sets a default Azure Storage service version that's different from
            // the ones expected by the Azure Stack Hub.  This sample illustrates how to work around this issue by using a pipeline
            // policy to request the Azure Storage SDK to change its service version.
            //
            // In order to do this, create a Blob Client Options instance and add a Storage Api Version Policy to it.  These options
            // will be passed to the Blob Container Client used by the processor.  The implementation of the policy is provided at
            // the end of this sample.

            string consumerGroup = EventHubConsumerClient.DefaultConsumerGroupName;

            BlobClientOptions storageClientOptions = new BlobClientOptions();

            storageClientOptions.AddPolicy(new StorageApiVersionPolicy(), HttpPipelinePosition.PerCall);

            BlobContainerClient  storageClient = new BlobContainerClient(blobStorageConnectionString, blobContainerName, storageClientOptions);
            EventProcessorClient processor     = new EventProcessorClient(storageClient, consumerGroup, eventHubsConnectionString, eventHubName);

            // For this example, we'll create a simple event handler that writes to the
            // console each time it was invoked.

            int eventsProcessed = 0;

            Task processEventHandler(ProcessEventArgs eventArgs)
            {
                if (eventArgs.CancellationToken.IsCancellationRequested)
                {
                    return(Task.CompletedTask);
                }

                if (eventArgs.HasEvent)
                {
                    ++eventsProcessed;
                    Console.WriteLine($"Event Received: { Encoding.UTF8.GetString(eventArgs.Data.Body.ToArray()) }");
                }

                return(Task.CompletedTask);
            }

            // For this example, exceptions will just be logged to the console.

            Task processErrorHandler(ProcessErrorEventArgs eventArgs)
            {
                if (eventArgs.CancellationToken.IsCancellationRequested)
                {
                    return(Task.CompletedTask);
                }

                Console.WriteLine("===============================");
                Console.WriteLine($"The error handler was invoked during the operation: { eventArgs.Operation ?? "Unknown" }, for Exception: { eventArgs.Exception.Message }");
                Console.WriteLine("===============================");
                Console.WriteLine();

                return(Task.CompletedTask);
            }

            processor.ProcessEventAsync += processEventHandler;
            processor.ProcessErrorAsync += processErrorHandler;

            try
            {
                // To begin, we'll publish a batch of events for our processor to receive. Because we are not specifying any routing hints,
                // the Event Hubs service will automatically route it to a single partition.

                var expectedEvents = new List <EventData>()
                {
                    new EventData(Encoding.UTF8.GetBytes("First Event, Single Batch")),
                    new EventData(Encoding.UTF8.GetBytes("Second Event, Single Batch")),
                    new EventData(Encoding.UTF8.GetBytes("Third Event, Single Batch"))
                };

                await using (var producer = new EventHubProducerClient(eventHubsConnectionString, eventHubName))
                {
                    using EventDataBatch eventBatch = await producer.CreateBatchAsync();

                    for (int index = 0; index < expectedEvents.Count; ++index)
                    {
                        eventBatch.TryAdd(expectedEvents[index]);
                    }

                    await producer.SendAsync(eventBatch);
                }

                Console.WriteLine("Starting the Event Processor client...");
                Console.WriteLine();

                await processor.StartProcessingAsync();

                using var cancellationSource = new CancellationTokenSource();
                cancellationSource.CancelAfter(TimeSpan.FromSeconds(90));

                while ((!cancellationSource.IsCancellationRequested) && (eventsProcessed < expectedEvents.Count))
                {
                    await Task.Delay(TimeSpan.FromMilliseconds(250));
                }

                // Once we arrive at this point, either cancellation was requested or we have processed all of our events.  In
                // both cases, we'll want to shut down the processor.

                Console.WriteLine();
                Console.WriteLine("Stopping the processor...");

                await processor.StopProcessingAsync();
            }
            finally
            {
                // It is encouraged that you unregister your handlers when you have finished
                // using the Event Processor to ensure proper cleanup.  This is especially
                // important when using lambda expressions or handlers in any form that may
                // contain closure scopes or hold other references.

                processor.ProcessEventAsync -= processEventHandler;
                processor.ProcessErrorAsync -= processErrorHandler;
            }

            // The Event Processor client has been stopped and is not explicitly disposable; there
            // is nothing further that we need to do for cleanup.

            Console.WriteLine();
        }