File tree Expand file tree Collapse file tree 1 file changed +16
-1
lines changed
Expand file tree Collapse file tree 1 file changed +16
-1
lines changed Original file line number Diff line number Diff line change @@ -38,7 +38,22 @@ internal static async Task<HttpRequestMessage> CloneAsync(this HttpRequestMessag
3838 await originalRequest . Content . CopyToAsync ( ms ) ;
3939 ms . Position = 0 ;
4040 newRequest . Content = new StreamContent ( ms ) ;
41- originalRequest . Content . Headers ? . ToList ( ) . ForEach ( header => newRequest . Content . Headers . TryAddWithoutValidation ( header . Key , header . Value ) ) ;
41+ // Attempt to copy request content headers with a single retry.
42+ // In .NET Framework, HttpHeaders dictionary is not thread safe. See https://github.com/dotnet/runtime/issues/61798.
43+ int retryCount = 0 ;
44+ int maxRetryCount = 2 ;
45+ while ( retryCount < maxRetryCount )
46+ {
47+ try
48+ {
49+ originalRequest . Content . Headers ? . ToList ( ) . ForEach ( header => newRequest . Content . Headers . TryAddWithoutValidation ( header . Key , header . Value ) ) ;
50+ retryCount = maxRetryCount ;
51+ }
52+ catch ( InvalidOperationException )
53+ {
54+ retryCount ++ ;
55+ }
56+ }
4257 }
4358 return newRequest ;
4459 }
You can’t perform that action at this time.
0 commit comments