Last post Oct 17, 2011 07:47 AM by levib
Oct 10, 2011 05:26 PM|mdonatas|LINK
In Async CTP preview there is sample called: (C# Anders) RSS Aggregator
And News.aspx.cs has this line:
protected async void Page_Load(object sender, EventArgs e)
Or here http://www.asp.net/learn/whitepapers/mvc4-release-notes#_Toc303253813 MVC4 example show how to use async controllers:
public async Task<ActionResult> Index(string city)
But using VS 11 Dev Preview I didn't have luck using these async features. In webforms in Page_Load setting Literal controls Text property has no effect. Same happens in MVC 4 for Dev Preview 11, when setting ViewBag.Message in async
action method, that is not reflected in the output. These scenarious work fine in non-async versions though.
Does anyone know why?
Oct 11, 2011 08:18 PM|xinqiu|LINK
This sample MVC4 aysnc should work, such as:
public class HomeController : AsyncController
public async Task<ActionResult> Index()
WebClient wc = new WebClient();
var result = await wc.DownloadStringTaskAsync("http://aspnet.codeplex.com/project/feeds/rss");
ViewBag.Message = "test: " + result;
For unit test:
ViewResult result = controller.Index() as ViewResult;
ViewResult result = controller.Index().GetAwaiter().GetResult() as ViewResult;
Oct 16, 2011 02:15 PM|mdonatas|LINK
Thanks, that did a thing for MVC, though I still couldn't find what's wrong with WebForms. Any help on WebForms async version of Page_Load is appreciated!
Oct 17, 2011 07:47 AM|levib|LINK
For WebForms, look at http://forums.asp.net/t/1722876.aspx for an example. Note: the Page_Load event is itself
synchronous, and you call RegisterAsyncTask to tell ASP.NET that it should invoke the asynchronous callback at the appropriate time. The asynchronous callback you pass to RegisterAsyncTask is invoked immediately after PreRender.