I have the below piece of code which I ran on .Net 4.7.2 and .Net Core but I have got different behavior for each framework
public class Program
{
private HttpClient Client = new HttpClient();
public static async Task Main(string[] args)
{
Program example = new Program();
Console.WriteLine("Starting connections");
int numberofIterations = 10;
Task<HttpResponseMessage>[] awaitableTasks = new Task<HttpResponseMessage>[numberofIterations];
for (int i = 0; i < numberofIterations; i++)
{
var httpRequestMessage = new HttpRequestMessage();
httpRequestMessage.RequestUri = new Uri("https://example.com");
httpRequestMessage.Method = new HttpMethod("GET");
awaitableTasks[i] = example.Client.SendAsync(httpRequestMessage);
//Console.WriteLine(result.StatusCode);
}
Console.WriteLine("Connections done");
await Task.WhenAll(awaitableTasks);
}
}
With the .Net Core framework, the network traces shows a separate tcp connection for each request while with the.NEt 4.7.2 framework the sockets get reused.
Network Trace .Net Core
Network Trace .Net 4.7.2
Appreciate your thoughts to understand the differences, to explain this behavior and the best way to overcome this issue.

