I've been getting this error randomly since last few months. I'm not sure what's the exact way to reproduce this, but the code hasn't changed in years, and this issue just happened suddenly..
System.ArgumentException: An item with the same key has already been added.
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
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.CreateAndAddHeaderToStore(String name)
at System.Net.Http.Headers.HttpHeaders.GetOrCreateHeaderInfo(String name, Boolean parseRawValues)
at System.Net.Http.Headers.HttpHeaderValueCollection`1.SetSpecialValue()
at System.Net.Http.Headers.HttpGeneralHeaders.set_ConnectionClose(Nullable`1 value)
All I'm trying to do is to set ConnectionClose to true or null, like this:
_httpClient.DefaultRequestHeaders.ConnectionClose = _closingConnections;
But then I'm getting errors about same key already added etc. How can I debug this?
Please advise.
Thanks!
Posting just for the sake of recommending the article because it gave me a clear picture of why it should not be done and how to proceed.
What is key to understand here is that although HttpClient is thread-safe, its DefaultRequestHeaders is not, which is why you're getting this error when multiple threads are trying to set the property. No matter what strategy you attempt to avoid the error, even if you're successful in capturing it, it will not be the most performant approach.
I recommend you read this article called Using HttpClient as it was intended, because you're not. It provides clear recommendations about how to use HttpClient depending on the different use cases, e.g. if you're always calling the same API, vs if you will be calling different APIs, etc.
In short, you should not be modifying DefaultRequestHeaders from concurrent code.
if (_client.DefaultRequestHeaders.Contains("ConnectionClose"))
{
_client.DefaultRequestHeaders.Remove("ConnectionClose");
}
_client.DefaultRequestHeaders.ConnectionClose = _closingConnections
Try to clean the value before reassign
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With