I have some trouble using HttpClient in multiple thread.
If i launch many download simultaneously, the first download of each thread is very slow (and increase with parallel threads)
For example, if I have one thread, everything is fine
First download Elapsed Time Download: 197 ms
Second download Elapsed Time Download: 171 ms
But with one thread, download time increase
First download Elapsed Time Download: 3881 ms
...
Second download Elapsed Time Download: 96 ms
...
Network bandwith is not an issue, i have same problem with localhost.
Here is some code to reproduce problem :
static void Main(string[] args)
{
ServicePointManager.DefaultConnectionLimit = 200;
List<Task> tasks = new List<Task>();
for (var i = 0; i < 10; i++)
{
tasks.Add(
Task.Factory.StartNew(() =>
{
Stopwatch s = Stopwatch.StartNew();
HttpClient httpClient = new HttpClient();
HttpResponseMessage httpDownloadResponse = httpDownloadResponse = httpClient.GetAsync("http://www.google.fr/", HttpCompletionOption.ResponseHeadersRead).Result;
s.Stop();
Console.WriteLine("First download Elapsed Time Download: {0} ms", s.ElapsedMilliseconds);
s = Stopwatch.StartNew();
httpClient = new HttpClient();
httpDownloadResponse = httpClient.GetAsync("http://www.google.fr/", HttpCompletionOption.ResponseHeadersRead).Result;
s.Stop();
Console.WriteLine("Second download Elapsed Time Download: {0} ms", s.ElapsedMilliseconds);
})
);
}
Task.WaitAll(tasks.ToArray());
while (Console.ReadLine() != null) ;
}