In Asp.Net Web Api 2 what is the difference between setting an HttpClient Accept Header using the following traditional method :
HttpClient client = HttpClientFactory.Create(handler);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
and the following method :
var headers = new Dictionary<string, string>
{
{"Accept", "application/json"}};
headers.ForEach(h => client.DefaultRequestHeaders.Add(h.Key, h.Value));
Update 1:
Based on the answer by @DarrenMiller in the following post What is the overhead of creating a new HttpClient per call in a WebAPI client? it appears that the preferred method is using DefaultRequestHeaders
property because it contains properties which are intended for multiple calls. Does this mean if I set a default header using a simple dictionary my HttpClient client
will not be as efficient as the one which uses DefaultRequestHeaders
? In addition I really cant understand how the values inside DefaultRequestHeaders
will be reused? Lets say I create 20 HttpClient client
using HttpClientFactory.Create
and inside every single one of them I set DefaultRequestHeaders
property [Do I really need to do it because DefaultRequestHeaders was meant to be reused?!]. Where does this reuse kick-in and does setting the DefaultRequestHeaders
every time I create a HttpClient client
result in some kind of performance hit?