Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# HttpHeaders - An item with the same key has already been added

Tags:

c#

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!

like image 237
Dhinnesh Jeevan Avatar asked Oct 23 '25 18:10

Dhinnesh Jeevan


2 Answers

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.

like image 158
GR7 Avatar answered Oct 26 '25 07:10

GR7


if (_client.DefaultRequestHeaders.Contains("ConnectionClose"))
{
            _client.DefaultRequestHeaders.Remove("ConnectionClose");
}
_client.DefaultRequestHeaders.ConnectionClose = _closingConnections

Try to clean the value before reassign

like image 31
Janaka Dissanayake Avatar answered Oct 26 '25 06:10

Janaka Dissanayake