这就是本质上正在发生的事情:
HttpMessageInvoker & HttpClient
class HttpMessageInvoker
{
private HttpMessageHandler handler;
public HttpMessageInvoker(HttpMessageHandler handler)
{
this.handler = handler;
}
public virtual void SendAsync()
{
Console.WriteLine("HttpMessageInvoker.SendAsync");
this.handler.SendAsync();
}
}
class HttpClient : HttpMessageInvoker
{
public HttpClient(HttpMessageHandler handler)
: base(handler)
{
}
public override void SendAsync()
{
Console.WriteLine("HttpClient.SendAsync");
base.SendAsync();
}
}
HttpMessageHandler 和 HttpClientHandler
abstract class HttpMessageHandler
{
protected internal abstract void SendAync();
}
class HttpClientHandler : HttpMessageHandler
{
protected internal override void SendAync()
{
Console.WriteLine("HttpClientHandler.SendAsync");
}
}
因此,如果您调用SendAsync
实例HttpClient
,则会执行该方法。该方法SendAsync
从 调用该方法HttpMessageInvoker
。该方法调用实例的SendAsync
方法。是抽象的;通过覆盖抽象方法来提供具体的实现。HttpMessageHandler
HttpMessageHandler
HttpClientHandler
SendAync
例子:
var handler = new HttpClientHandler();
var client = new HttpClient(handler);
client.SendAsync();
输出:
HttpClient.SendAsync
HttpMessageInvoker.SendAsync
HttpClientHandler.SendAsync