简体   繁体   English

如何在ASP.NET 4.0中将HttpClient异步方法与WebForm的RegisterAsyncTask一起使用?

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

I am using HttpClient in ASP.NET framework 4.0. 我在ASP.NET Framework 4.0中使用HttpClient。 All example are I have seen using OnBeginXX/OnEndXXX or async/await(4.5) with RegisterAsyncTask. 我见过所有示例,将OnBeginXX / OnEndXXX或async / await(4.5)与RegisterAsyncTask一起使用。 I need to ask how to make RegisterAsyncTask work with HtpClient's GetByteArrayAsync(or similar). 我需要问如何使RegisterAsyncTask与HtpClient的GetByteArrayAsync(或类似)一起使用。 Note this is ASP.NET 4.0? 注意这是ASP.NET 4.0吗?

Update: Just found that in 4.5 you have more overloads than 2.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. 

Any other solution? 还有其他解决方案吗?

Update 2: I think TPL is not supported in 4.0 with recommended RegisterAsyncTask. 更新2:我认为建议的RegisterAsyncTask在4.0中不支持TPL。 Need to use HttpWebRequest with RegisterAssyncTask 需要将HttpWebRequest与RegisterAssyncTask一起使用

You cannot use async / await on ASP.NET 4.0. 您不能在ASP.NET 4.0上使用async / await This probably means you cannot use HttpClient either. 这可能意味着您也不能使用HttpClient

So, I recommend using WebClient or HttpWebRequest . 因此,我建议使用WebClientHttpWebRequest These support older asynchronous patterns that ASP.NET 4.0 understands. 这些支持ASP.NET 4.0可以理解的较旧的异步模式。

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

it is work in .net 4.0 它在.net 4.0中工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM