這是發生了什麼本質:
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
實例上,該方法被執行。該方法從HttpMessageInvoker
調用SendAsync
方法。該方法調用HttpMessageHandler
實例的SendAsync
方法。 HttpMessageHandler
是抽象的; HttpClientHandler
通過覆蓋它來提供抽象SendAync
方法的具體實現。
例子:
var handler = new HttpClientHandler();
var client = new HttpClient(handler);
client.SendAsync();
輸出:
HttpClient.SendAsync
HttpMessageInvoker.SendAsync
HttpClientHandler.SendAsync
來源
2013-01-07 08:53:13
dtb
總結你的問題:HttpClient的有一個方法SendAsync。 HttpMessageHandler有一個方法SendAsync(由HttpClientHandler覆蓋)。 HttpClient.SendAsync調用HttpMessageHandler.SendAync。有什麼問題? – dtb
你的意思是HttpClient.SendAsync調用HttpMessageHandler的SendAsync方法,而不是它自己的? – Allen4Tech
當您調用HttpClient.SendAsync時,則執行HttpClient.SendAsync。當執行HttpClient.SendAsync時,它會調用HttpMessageHandler.SendAsync。 – dtb