Exemplo n.º 1
0
        /// <summary>
        /// Creates an instance of <see cref="HttpPipeline"/> populated with default policies, customer provided policies from <paramref name="options"/> and client provided per call policies.
        /// </summary>
        /// <param name="options">The customer provided client options object.</param>
        /// <param name="perCallPolicies">Client provided per-call policies.</param>
        /// <param name="perRetryPolicies">Client provided per-retry policies.</param>
        /// <param name="responseClassifier">The client provided response classifier.</param>
        /// <returns>A new instance of <see cref="HttpPipeline"/></returns>
        public static HttpPipeline Build(ClientOptions options, HttpPipelinePolicy[] perCallPolicies, HttpPipelinePolicy[] perRetryPolicies, ResponseClassifier responseClassifier)
        {
            if (perCallPolicies == null)
            {
                throw new ArgumentNullException(nameof(perCallPolicies));
            }

            if (perRetryPolicies == null)
            {
                throw new ArgumentNullException(nameof(perRetryPolicies));
            }

            var policies = new List <HttpPipelinePolicy>();

            bool isDistributedTracingEnabled = options.Diagnostics.IsDistributedTracingEnabled;

            policies.Add(ReadClientRequestIdPolicy.Shared);

            policies.AddRange(perCallPolicies);

            policies.AddRange(options.PerCallPolicies);

            policies.Add(ClientRequestIdPolicy.Shared);

            DiagnosticsOptions diagnostics = options.Diagnostics;

            if (diagnostics.IsTelemetryEnabled)
            {
                policies.Add(CreateTelemetryPolicy(options));
            }

            RetryOptions retryOptions = options.Retry;

            policies.Add(new RetryPolicy(retryOptions.Mode, retryOptions.Delay, retryOptions.MaxDelay, retryOptions.MaxRetries));

            policies.Add(RedirectPolicy.Shared);

            policies.AddRange(perRetryPolicies);

            policies.AddRange(options.PerRetryPolicies);

            if (diagnostics.IsLoggingEnabled)
            {
                string assemblyName = options.GetType().Assembly !.GetName().Name !;

                policies.Add(new LoggingPolicy(diagnostics.IsLoggingContentEnabled, diagnostics.LoggedContentSizeLimit,
                                               diagnostics.LoggedHeaderNames.ToArray(), diagnostics.LoggedQueryParameters.ToArray(), assemblyName));
            }

            policies.Add(new ResponseBodyPolicy(options.Retry.NetworkTimeout));

            policies.Add(new RequestActivityPolicy(isDistributedTracingEnabled, ClientDiagnostics.GetResourceProviderNamespace(options.GetType().Assembly)));

            policies.RemoveAll(policy => policy == null);

            return(new HttpPipeline(options.Transport,
                                    policies.ToArray(),
                                    responseClassifier));
        }
        public static HttpPipeline Build(ClientOptions options, HttpPipelinePolicy[] perCallClientPolicies, HttpPipelinePolicy[] perRetryClientPolicies, ResponseClassifier responseClassifier)
        {
            if (perCallClientPolicies == null)
            {
                throw new ArgumentNullException(nameof(perCallClientPolicies));
            }

            if (perRetryClientPolicies == null)
            {
                throw new ArgumentNullException(nameof(perRetryClientPolicies));
            }

            var policies = new List <HttpPipelinePolicy>();

            bool isDistributedTracingEnabled = options.Diagnostics.IsDistributedTracingEnabled;

            policies.AddRange(perCallClientPolicies);

            policies.AddRange(options.PerCallPolicies);

            policies.Add(ClientRequestIdPolicy.Shared);

            DiagnosticsOptions diagnostics = options.Diagnostics;

            if (diagnostics.IsTelemetryEnabled)
            {
                policies.Add(CreateTelemetryPolicy(options));
            }

            RetryOptions retryOptions = options.Retry;

            policies.Add(new RetryPolicy(retryOptions.Mode, retryOptions.Delay, retryOptions.MaxDelay, retryOptions.MaxRetries));

            policies.AddRange(perRetryClientPolicies);

            policies.AddRange(options.PerRetryPolicies);

            if (diagnostics.IsLoggingEnabled)
            {
                policies.Add(new LoggingPolicy(diagnostics.IsLoggingContentEnabled, diagnostics.LoggingContentSizeLimit,
                                               diagnostics.LoggingAllowedHeaderNames.ToArray(), diagnostics.LoggingAllowedQueryParameters.ToArray()));
            }

            policies.Add(BufferResponsePolicy.Shared);

            policies.Add(new RequestActivityPolicy(isDistributedTracingEnabled));

            policies.RemoveAll(policy => policy == null);

            return(new HttpPipeline(options.Transport,
                                    policies.ToArray(),
                                    responseClassifier,
                                    new ClientDiagnostics(isDistributedTracingEnabled)));
        }
        internal static (ResponseClassifier Classifier, HttpPipelineTransport Transport, int PerCallIndex, int PerRetryIndex, HttpPipelinePolicy[] Policies, bool IsTransportOwned) BuildInternal(
            ClientOptions options,
            HttpPipelinePolicy[] perCallPolicies,
            HttpPipelinePolicy[] perRetryPolicies,
            HttpPipelineTransportOptions?defaultTransportOptions,
            ResponseClassifier?responseClassifier)
        {
            if (perCallPolicies == null)
            {
                throw new ArgumentNullException(nameof(perCallPolicies));
            }

            if (perRetryPolicies == null)
            {
                throw new ArgumentNullException(nameof(perRetryPolicies));
            }

            var policies = new List <HttpPipelinePolicy>(8 +
                                                         (options.Policies?.Count ?? 0) +
                                                         perCallPolicies.Length +
                                                         perRetryPolicies.Length);

            void AddCustomerPolicies(HttpPipelinePosition position)
            {
                if (options.Policies != null)
                {
                    foreach (var policy in options.Policies)
                    {
                        if (policy.Position == position)
                        {
                            policies.Add(policy.Policy);
                        }
                    }
                }
            }

            DiagnosticsOptions diagnostics = options.Diagnostics;

            var sanitizer = new HttpMessageSanitizer(diagnostics.LoggedQueryParameters.ToArray(), diagnostics.LoggedHeaderNames.ToArray());

            bool isDistributedTracingEnabled = options.Diagnostics.IsDistributedTracingEnabled;

            policies.Add(ReadClientRequestIdPolicy.Shared);

            policies.AddRange(perCallPolicies);

            AddCustomerPolicies(HttpPipelinePosition.PerCall);

            policies.RemoveAll(static policy => policy == null);
        internal static (ResponseClassifier Classifier, HttpPipelineTransport Transport, int PerCallIndex, int PerRetryIndex, HttpPipelinePolicy[] Policies, bool IsTransportOwned) BuildInternal(
            ClientOptions options,
            HttpPipelinePolicy[] perCallPolicies,
            HttpPipelinePolicy[] perRetryPolicies,
            HttpPipelineTransportOptions?defaultTransportOptions,
            ResponseClassifier?responseClassifier)
        {
            if (perCallPolicies == null)
            {
                throw new ArgumentNullException(nameof(perCallPolicies));
            }

            if (perRetryPolicies == null)
            {
                throw new ArgumentNullException(nameof(perRetryPolicies));
            }

            var policies = new List <HttpPipelinePolicy>(8 +
                                                         (options.Policies?.Count ?? 0) +
                                                         perCallPolicies.Length +
                                                         perRetryPolicies.Length);

            void AddCustomerPolicies(HttpPipelinePosition position)
            {
                if (options.Policies != null)
                {
                    foreach (var policy in options.Policies)
                    {
                        // skip null policies to ensure that calculations for perCallIndex and perRetryIndex are accurate
                        if (policy.Position == position && policy.Policy != null)
                        {
                            policies.Add(policy.Policy);
                        }
                    }
                }
            }

            // A helper to ensure that we only add non-null policies to the policies list
            // This ensures that calculations for perCallIndex and perRetryIndex are accurate
            void AddNonNullPolicies(HttpPipelinePolicy[] policiesToAdd)
            {
                for (int i = 0; i < policiesToAdd.Length; i++)
                {
                    var policy = policiesToAdd[i];
                    if (policy != null)
                    {
                        policies.Add(policy);
                    }
                }
            }

            DiagnosticsOptions diagnostics = options.Diagnostics;

            var sanitizer = new HttpMessageSanitizer(diagnostics.LoggedQueryParameters.ToArray(), diagnostics.LoggedHeaderNames.ToArray());

            bool isDistributedTracingEnabled = options.Diagnostics.IsDistributedTracingEnabled;

            policies.Add(ReadClientRequestIdPolicy.Shared);

            AddNonNullPolicies(perCallPolicies);

            AddCustomerPolicies(HttpPipelinePosition.PerCall);

            var perCallIndex = policies.Count;

            policies.Add(ClientRequestIdPolicy.Shared);

            if (diagnostics.IsTelemetryEnabled)
            {
                policies.Add(CreateTelemetryPolicy(options));
            }

            RetryOptions retryOptions = options.Retry;

            policies.Add(new RetryPolicy(retryOptions.Mode, retryOptions.Delay, retryOptions.MaxDelay, retryOptions.MaxRetries));

            policies.Add(RedirectPolicy.Shared);

            AddNonNullPolicies(perRetryPolicies);

            AddCustomerPolicies(HttpPipelinePosition.PerRetry);

            var perRetryIndex = policies.Count;

            if (diagnostics.IsLoggingEnabled)
            {
                string assemblyName = options.GetType().Assembly !.GetName().Name !;

                policies.Add(new LoggingPolicy(diagnostics.IsLoggingContentEnabled, diagnostics.LoggedContentSizeLimit, sanitizer, assemblyName));
            }

            policies.Add(new ResponseBodyPolicy(options.Retry.NetworkTimeout));

            policies.Add(new RequestActivityPolicy(isDistributedTracingEnabled, ClientDiagnostics.GetResourceProviderNamespace(options.GetType().Assembly), sanitizer));

            AddCustomerPolicies(HttpPipelinePosition.BeforeTransport);

            // Override the provided Transport with the provided transport options if the transport has not been set after default construction and options are not null.
            HttpPipelineTransport transport   = options.Transport;
            bool isTransportInternallyCreated = false;

            if (defaultTransportOptions != null)
            {
                if (options.IsCustomTransportSet)
                {
                    if (AzureCoreEventSource.Singleton.IsEnabled())
                    {
                        // Log that we were unable to override the custom transport
                        AzureCoreEventSource.Singleton.PipelineTransportOptionsNotApplied(options?.GetType().FullName ?? String.Empty);
                    }
                }
                else
                {
                    transport = HttpPipelineTransport.Create(defaultTransportOptions);
                    isTransportInternallyCreated = true;
                }
            }

            policies.Add(new HttpPipelineTransportPolicy(transport, sanitizer));

            responseClassifier ??= ResponseClassifier.Shared;

            return(responseClassifier, transport, perCallIndex, perRetryIndex, policies.ToArray(), isTransportInternallyCreated);
        }
Exemplo n.º 5
0
 protected ClientOptions()
 {
     Retry       = new RetryOptions();
     Diagnostics = new DiagnosticsOptions();
 }
Exemplo n.º 6
0
        /// <summary>
        /// Creates an instance of <see cref="HttpPipeline"/> populated with default policies, customer provided policies from <paramref name="options"/> and client provided per call policies.
        /// </summary>
        /// <param name="options">The customer provided client options object.</param>
        /// <param name="perCallPolicies">Client provided per-call policies.</param>
        /// <param name="perRetryPolicies">Client provided per-retry policies.</param>
        /// <param name="responseClassifier">The client provided response classifier.</param>
        /// <returns>A new instance of <see cref="HttpPipeline"/></returns>
        public static HttpPipeline Build(ClientOptions options, HttpPipelinePolicy[] perCallPolicies, HttpPipelinePolicy[] perRetryPolicies, ResponseClassifier responseClassifier)
        {
            if (perCallPolicies == null)
            {
                throw new ArgumentNullException(nameof(perCallPolicies));
            }

            if (perRetryPolicies == null)
            {
                throw new ArgumentNullException(nameof(perRetryPolicies));
            }

            var policies = new List <HttpPipelinePolicy>(8 +
                                                         (options.Policies?.Count ?? 0) +
                                                         perCallPolicies.Length +
                                                         perRetryPolicies.Length);

            void AddCustomerPolicies(HttpPipelinePosition position)
            {
                if (options.Policies != null)
                {
                    foreach (var policy in options.Policies)
                    {
                        if (policy.Position == position)
                        {
                            policies.Add(policy.Policy);
                        }
                    }
                }
            }

            DiagnosticsOptions diagnostics = options.Diagnostics;

            var sanitizer = new HttpMessageSanitizer(diagnostics.LoggedHeaderNames.ToArray(), diagnostics.LoggedQueryParameters.ToArray());

            bool isDistributedTracingEnabled = options.Diagnostics.IsDistributedTracingEnabled;

            policies.Add(ReadClientRequestIdPolicy.Shared);

            policies.AddRange(perCallPolicies);

            AddCustomerPolicies(HttpPipelinePosition.PerCall);

            policies.Add(ClientRequestIdPolicy.Shared);

            if (diagnostics.IsTelemetryEnabled)
            {
                policies.Add(CreateTelemetryPolicy(options));
            }

            RetryOptions retryOptions = options.Retry;

            policies.Add(new RetryPolicy(retryOptions.Mode, retryOptions.Delay, retryOptions.MaxDelay, retryOptions.MaxRetries));

            policies.Add(RedirectPolicy.Shared);

            policies.AddRange(perRetryPolicies);

            AddCustomerPolicies(HttpPipelinePosition.PerRetry);

            if (diagnostics.IsLoggingEnabled)
            {
                string assemblyName = options.GetType().Assembly !.GetName().Name !;

                policies.Add(new LoggingPolicy(diagnostics.IsLoggingContentEnabled, diagnostics.LoggedContentSizeLimit, sanitizer, assemblyName));
            }

            policies.Add(new ResponseBodyPolicy(options.Retry.NetworkTimeout));

            policies.Add(new RequestActivityPolicy(isDistributedTracingEnabled, ClientDiagnostics.GetResourceProviderNamespace(options.GetType().Assembly), sanitizer));

            AddCustomerPolicies(HttpPipelinePosition.BeforeTransport);

            policies.RemoveAll(static policy => policy == null);
Exemplo n.º 7
0
 protected ClientOptions()
 {
     Retry              = new RetryOptions();
     Diagnostics        = new DiagnosticsOptions();
     ResponseClassifier = new ResponseClassifier();
 }
Exemplo n.º 8
0
 /// <summary>
 /// </summary>
 public ExceptionFormattingResponseClassifier(ResponseClassifier responseClassifier, DiagnosticsOptions diagnostics)
 {
     _responseClassifier = responseClassifier;
     MessageSanitizer    = ClientDiagnostics.CreateMessageSanitizer(diagnostics);
 }