I have a search action that has two paths, a synchronous path, that just returns a simple view, and a asynchronous path, that does the search asynchronously and then returns a view. Both are GET requests, so they are part of the same action.
The problem is that when I access the action "localhost:XXXX/Home/Search", the page just infinitely loads. Using Fiddler, I can see that the request never returns. I've debugged it and it makes it to the last line of code, but, again, the request doesn't
complete.
I've simplified the repro to the following:
public async Task<ActionResult> Search()
{
return View();
}
VS11 warns me that the code will be run synchronously without an await, which is fine, but the request doesn't complete.
Should this work? Or do I need to something else here?
This was a bug in the MVC 4 Beta that we have fixed for the next release. I'm following up to see if there's a workaround you can use in the meantime (aside from just not using async).
MisinformedD...
Member
9 Points
23 Posts
Using an Async Action to Run Synchronous Code
Mar 08, 2012 01:04 PM|LINK
I have a search action that has two paths, a synchronous path, that just returns a simple view, and a asynchronous path, that does the search asynchronously and then returns a view. Both are GET requests, so they are part of the same action.
The problem is that when I access the action "localhost:XXXX/Home/Search", the page just infinitely loads. Using Fiddler, I can see that the request never returns. I've debugged it and it makes it to the last line of code, but, again, the request doesn't complete.
I've simplified the repro to the following:
public async Task<ActionResult> Search() { return View(); }VS11 warns me that the code will be run synchronously without an await, which is fine, but the request doesn't complete.
Should this work? Or do I need to something else here?
Eilon
Contributor
5753 Points
976 Posts
Microsoft
Re: Using an Async Action to Run Synchronous Code
Mar 15, 2012 03:39 AM|LINK
Hi,
This was a bug in the MVC 4 Beta that we have fixed for the next release. I'm following up to see if there's a workaround you can use in the meantime (aside from just not using async).
Thanks,
Eilon
levib
Star
7702 Points
1099 Posts
Microsoft
Re: Using an Async Action to Run Synchronous Code
Mar 16, 2012 04:09 AM|LINK
To work around this issue for the Beta, do two things. First, add the following to your ~/Web.config if it is not already there:
<appSettings> <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> </appSettings>Second, add this as the first line within your Task-returning action method:
The second part (the 'await Task.Yield();') will not be necessary once this bug is fixed post-Beta.
Hope this helps!