We have been seeing the following stack trace in our code base:
System.ArgumentException: An item with the same key has already been added.
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at System.Net.Http.Headers.HttpHeaders.AddHeaderToStore(String name, HeaderStoreItemInfo info)
at System.Net.Http.Headers.HttpHeaders.SetParsedValue(String name, Object value)
at System.Net.Http.Headers.HttpContentHeaders.get_ContentLength()
at System.Net.Http.HttpClientHandler.PrepareAndStartContentUpload(RequestState state)
and have narrowed it down to somewhere in our code where we call
HttpContent.ReadAsStringAsync()
When this happens, we are sure the instance of HttpContent is being used by multiple threads, all of which are somehow trying to read the actual content. Have not yet figured out how reading of the content affects the headers.
If it was possible to perform a deep clone of the HttpContent, assuming all if the content payload has been downloaded, we would have looked into this option.
Anyone run into this and if so, how did you solve it?
Thanks in advance.