HttpClient
WebAPI客户端的生存期应该是什么?
拥有HttpClient
多个通话的实例更好吗?
创建和处理HttpClient
每个请求的开销是多少,如以下示例所示(摘自http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from- a-net-client):
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:9000/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// New code:
HttpResponseMessage response = await client.GetAsync("api/products/1");
if (response.IsSuccessStatusCode)
{
Product product = await response.Content.ReadAsAsync<Product>();
Console.WriteLine("{0}\t${1}\t{2}", product.Name, product.Price, product.Category);
}
}
Stopwatch
该类对其进行基准测试。我的估计是HttpClient
,假设所有这些实例都在同一上下文中使用,则拥有一个就更有意义。