簡體   English   中英

如何在ASP.NET 4.0中將HttpClient異步方法與WebForm的RegisterAsyncTask一起使用?

[英]How to use HttpClient async methods with WebForm's RegisterAsyncTask in ASP.NET 4.0?

我在ASP.NET Framework 4.0中使用HttpClient。 我見過所有示例,將OnBeginXX / OnEndXXX或async / await(4.5)與RegisterAsyncTask一起使用。 我需要問如何使RegisterAsyncTask與HtpClient的GetByteArrayAsync(或類似)一起使用。 注意這是ASP.NET 4.0嗎?

更新:剛發現4.5中的重載比2.0多

Public method   PageAsyncTask(Func<Task>)   Initializes a new instance of the PageAsyncTask class using an event handler that enables the task to be handled.
Public method   PageAsyncTask(Func<CancellationToken, Task>)    Initializes a new instance of the PageAsyncTask class using an event handler that enables the task to be canceled.
Public method   PageAsyncTask(BeginEventHandler, EndEventHandler, EndEventHandler, Object)  Initializes a new instance of the PageAsyncTask class using the default value for executing in parallel.
Public method   PageAsyncTask(BeginEventHandler, EndEventHandler, EndEventHandler, Object, Boolean) Initializes a new instance of the PageAsyncTask class using the specified value for executing in parallel. 

還有其他解決方案嗎?

更新2:我認為建議的RegisterAsyncTask在4.0中不支持TPL。 需要將HttpWebRequest與RegisterAssyncTask一起使用

您不能在ASP.NET 4.0上使用async / await 這可能意味着您也不能使用HttpClient

因此,我建議使用WebClientHttpWebRequest 這些支持ASP.NET 4.0可以理解的較舊的異步模式。

var client = new HttpClient();
client.GetByteArrayAsync("url").ContinueWith(t => {
    //insert your code here
});

它在.net 4.0中工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM