Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use async/await to achieve asynchronous page in asp.net webform?

We can now use the async/await key words in ASP.NET MVC 4.

public async Task<ActionResult> TestAsync()
{
    WebClient client = new WebClient();
    return Content(await client.DownloadStringTaskAsync("http://www.google.com"));
}

But how to use it in ASP.NET WebForms?

like image 914
Dozer Avatar asked Mar 05 '12 04:03

Dozer


People also ask

How use async await in asp net?

You can use the await keyword only in methods annotated with the async keyword. The await keyword does not block the thread until the task is complete. It signs up the rest of the method as a callback on the task, and immediately returns.

How is async await asynchronous?

An async function consists of two main keywords async and await. They keyword async is used to make a function asynchronous. The await keyword will ask the execution to wait until the defined task gets executed. It allows the use of await Keyword inside the functions with async keyword.

How does async await work in Web API?

An asynchronous method allows you to start a long-running operation, returns your thread to the pool, and wakes up on a different thread or the same depending on the availability of threads in the pool at that time. Now create an application. Create a Web API application as in the following: Start Visual Studio 2012.

How do you call async method in page load?

How can I call a async method on Page_Load ? If you change the method to static async Task instead of void, you can call it by using SendTweetWithSinglePicture("test", "path"). Wait() . Avoid async void unless you are using it for events.


2 Answers

One easy way is to just make your event handlers async. First, add the Async="true" parameter to the @Page directive, and then you should be able to write async event handlers as such:

protected async void Page_Load(object sender, EventArgs e)
{
  var client = new WebClient();
  var content = await client.DownloadStringTaskAsync("http://www.google.com");
  Response.Write(content);
}

I say "should be able to" because I haven't actually tried this out myself. But it should work.

Update: This does not work for Page_Load (see this MSDN forum thread), but should work for other events such as button clicks.

Update: This does work for Page_Load in ASP.NET 4.5. Also, they added checks if you improperly use an async event handler. See this video for details.

like image 143
Stephen Cleary Avatar answered Sep 18 '22 14:09

Stephen Cleary


According to http://www.hanselman.com/blog/TheMagicOfUsingAsynchronousMethodsInASPNET45PlusAnImportantGotcha.aspx the only reliable way to use async in web forms is to call Page.RegisterAsyncTask.

The code to support simple things like async Page_Load is extremely complicated and not well-tested for anything beyond basic scenarios.

Using async with voids is not stable or reliable. However, all you have to do is call Page.RegisterAyncTask - it's not any trouble and you'll be in a better more flexible place

.

public void Page_Load(object sender, EventArgs e)
{
    RegisterAsyncTask(new PageAsyncTask(LoadSomeData));
}

public async Task LoadSomeData()
{
var clientcontacts = Client.DownloadStringTaskAsync("api/contacts");
var clienttemperature = Client.DownloadStringTaskAsync("api/temperature");

var contacts = Newtonsoft.Json.JsonConvert.DeserializeObject>(await clientcontacts);
var temperature = Newtonsoft.Json.JsonConvert.DeserializeObject(await clienttemperature);

listcontacts.DataSource = contacts;
listcontacts.DataBind();
Temparature.Text = temperature; 
}
like image 30
Michael Freidgeim Avatar answered Sep 21 '22 14:09

Michael Freidgeim