@@ -88,14 +88,29 @@ void AddCustomerPolicies(HttpPipelinePosition position)
8888 {
8989 foreach ( var policy in options . Policies )
9090 {
91- if ( policy . Position == position )
91+ // skip null policies to ensure that calculations for perCallIndex and perRetryIndex are accurate
92+ if ( policy . Position == position && policy . Policy != null )
9293 {
9394 policies . Add ( policy . Policy ) ;
9495 }
9596 }
9697 }
9798 }
9899
100+ // A helper to ensure that we only add non-null policies to the policies list
101+ // This ensures that calculations for perCallIndex and perRetryIndex are accurate
102+ void AddNonNullPolicies ( HttpPipelinePolicy [ ] policiesToAdd )
103+ {
104+ for ( int i = 0 ; i < policiesToAdd . Length ; i ++ )
105+ {
106+ var policy = policiesToAdd [ i ] ;
107+ if ( policy != null )
108+ {
109+ policies . Add ( policy ) ;
110+ }
111+ }
112+ }
113+
99114 DiagnosticsOptions diagnostics = options . Diagnostics ;
100115
101116 var sanitizer = new HttpMessageSanitizer ( diagnostics . LoggedQueryParameters . ToArray ( ) , diagnostics . LoggedHeaderNames . ToArray ( ) ) ;
@@ -104,11 +119,10 @@ void AddCustomerPolicies(HttpPipelinePosition position)
104119
105120 policies . Add ( ReadClientRequestIdPolicy . Shared ) ;
106121
107- policies . AddRange ( perCallPolicies ) ;
122+ AddNonNullPolicies ( perCallPolicies ) ;
108123
109124 AddCustomerPolicies ( HttpPipelinePosition . PerCall ) ;
110125
111- policies . RemoveAll ( static policy => policy == null ) ;
112126 var perCallIndex = policies . Count ;
113127
114128 policies . Add ( ClientRequestIdPolicy . Shared ) ;
@@ -123,12 +137,10 @@ void AddCustomerPolicies(HttpPipelinePosition position)
123137
124138 policies . Add ( RedirectPolicy . Shared ) ;
125139
126- policies . AddRange ( perRetryPolicies ) ;
140+ AddNonNullPolicies ( perRetryPolicies ) ;
127141
128142 AddCustomerPolicies ( HttpPipelinePosition . PerRetry ) ;
129143
130- policies . RemoveAll ( static policy => policy == null ) ;
131-
132144 var perRetryIndex = policies . Count ;
133145
134146 if ( diagnostics . IsLoggingEnabled )
@@ -143,7 +155,6 @@ void AddCustomerPolicies(HttpPipelinePosition position)
143155 policies . Add ( new RequestActivityPolicy ( isDistributedTracingEnabled , ClientDiagnostics . GetResourceProviderNamespace ( options . GetType ( ) . Assembly ) , sanitizer ) ) ;
144156
145157 AddCustomerPolicies ( HttpPipelinePosition . BeforeTransport ) ;
146- policies . RemoveAll ( static policy => policy == null ) ;
147158
148159 // Override the provided Transport with the provided transport options if the transport has not been set after default construction and options are not null.
149160 HttpPipelineTransport transport = options . Transport ;
0 commit comments