locked
Visual Studio 11 Beta async action methods hang indefinitely

    Question

  • Under Visual Studio 11 Developer Preview, async action methods worked correctly without issue. After installing Visual Studio 11 Beta, those same async action methods now cause the page to hang indefinitely.

    public class HomeController : AsyncController {
        public async Task<ActionResult> Index() {
            return View();
        }
    }
    

    Change the action back to synchronous and it works without hanging.

    public class HomeController : Controller {
        public ActionResult Index() {
            return View();
        }
    }
    

    This blog post (http://blogs.msdn.com/b/pfxteam/archive/2012/03/03/10277166.aspx) makes me think the problem has to do with the beta however, the site is built targeting .NET Framework 4.5, not 4.0 so presumably it should work.

    "This means that while you can absolutely use Visual Studio 11 Beta to build apps that use async/await with .NET 4.5, you are currently unable to use Visual Studio 11 Beta to build apps that use async/await against .NET 4, Silverlight 5, etc., since the compilers in Visual Studio 11 Beta are expecting differently shaped types than those in the existing AsyncCtpLibrary* DLLs."

    Any suggestions on where to look for a fix?

    Monday, March 05, 2012 1:14 AM

Answers

  • Gene and I followed up offline.  For anyone else reading and interested...

    The short answer is that there is a known bug in ASP.NET MVC in the .NET 4.5 Beta that results in this issue when the async method completes synchronously.  Until a fix is available, a simple workaround is to add "await Task.Yield();" as the first line of the async method, forcing it to complete asynchronously.  For this to work correctly, you also need to ensure you're using the new SynchronizationContext supplied by ASP.NET in .NET 4.5, which means ensuring you have the line:
        <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    in the appSettings section of your configuration file.

    Tuesday, March 06, 2012 3:58 AM
    Moderator

All replies

  • First - a couple of questions:

    1) You're trying to use async without "await" inside of the method body.  What is the goal here?  Is this your real, complete code? 

    2) How are you calling this?  It's difficult to see what all could cause a hang without seeing how you're trying to use the method itself.  Are you awaiting this in a UI thread call?

    3) Given the names, I would guess "View()" is performing a UI-related operation.  If this is the case, it may not make sense to make this (at least entirely) asynchronous.  The problem could be as simple as creating the user interface on a background thread, which isn't going to work, as user interfaces (typically) have thread affinity.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Monday, March 05, 2012 5:35 PM
    Moderator
  • Gene and I followed up offline.  For anyone else reading and interested...

    The short answer is that there is a known bug in ASP.NET MVC in the .NET 4.5 Beta that results in this issue when the async method completes synchronously.  Until a fix is available, a simple workaround is to add "await Task.Yield();" as the first line of the async method, forcing it to complete asynchronously.  For this to work correctly, you also need to ensure you're using the new SynchronizationContext supplied by ASP.NET in .NET 4.5, which means ensuring you have the line:
        <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    in the appSettings section of your configuration file.

    Tuesday, March 06, 2012 3:58 AM
    Moderator
  • Sthephen,

    Should I set UseTaskFriendlySynchronizationContext to true when creating new WebApps?

    Thx


    http://blog.fujiy.net/ - MCPD em .NET 2.0 MCTS em .NET 4

    Tuesday, October 02, 2012 2:13 PM