[英]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
。
因此,我建议使用WebClient
或HttpWebRequest
。 这些支持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.