I want to have WebApi service so I must use HttpClient or WebClient library for Client Application
HttpClient is modern and async and it my choice but I checked performance of it
Unfortunately, CRUD operations speed is much lower than WebClient library , Especially in the first call and use
I test it with simple sample with CRUD Operations (Get-Post-Put-Delete) - same result ; performance is disappointing than WebClient
WHY ???
see this picture that get 120 records from DB with HttpClient (async) and WebClient (sync)
this is my codes :
// Sync - WebClient
public static IList<Person> GetPersonList()
{
using (var client = new WebClient())
{
client.BaseAddress = @"http://localhost:9810/";
var str = client.DownloadString("api/person/get");
return JsonConvert.DeserializeObject<List<Person>>(str);
}
}
// Async - HttpClient
public static async Task<IList<Person>> GetPersonAsync()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:9810/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync("api/person/get").ConfigureAwait(false);
if (response.IsSuccessStatusCode)
return await response.Content.ReadAsAsync<IList<Person>>().ConfigureAwait(false);
return null;
}
}
testing method :
var s1 = new Stopwatch();
var s2 = new Stopwatch();
s1.Start();
var p = ClientPerson.GetPersonAsync().Result;
s1.Stop();
s2.Start();
var pp = ClientPerson.GetPersonList();
s2.Stop();
label1.Text = "Async : " + s1.Elapsed.TotalSeconds
+ " | Sync : " + s2.Elapsed.TotalSeconds;