1

I'm using Visual Studio 2011 Beta with 4.5 Beta. There seems to be a bug with ASP.Net MVC 4, where if the method returns a none "TaskAsync" task, it hangs the request.

public class HomeController : Controller
{
    //
    // GET: /Home/

    public async Task<ActionResult> Test1()
    {
        string s = await new WebClient().DownloadStringTaskAsync("http://google.com");
        return Content("asdf");
    }

    public async Task<ActionResult> Test2()
    {
        string MyConString = ConfigurationManager.ConnectionStrings["Master"].ConnectionString;
        MySqlConnection connection = new MySqlConnection(MyConString);
        await connection.OpenAsync();
        connection.Close();
        return Content("asdf");
    }
}

Test1 works fine. Test2 hangs once the method returns. I am able to debug through the code with no errors.

Anyone know a fix/workaround for this?

svick
  • 236,525
  • 50
  • 385
  • 514
user896993
  • 1,341
  • 13
  • 15

1 Answers1

4

Known issue with MVC 4 Beta.

In short, add the following to ~/Web.config:

  <appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> 
  </appSettings> 

Then add await Task.Yield(); as the first line in your action method. (Don't forget the await!)

svick
  • 236,525
  • 50
  • 385
  • 514
Levi
  • 32,628
  • 3
  • 87
  • 88
  • Does this work when the async & await is used internally, but not all the way up to the controller action method? (i.e. code called from the action method waits for the Task.Result of an async method) If it should work in that case then I'm doing something wrong, it locks up cold around that point. ISS wandering off & the browser staying at "Waiting for www.testwebsite.local" until you close it is a time-consuming & hard to debug error mode. – Anthony Jul 06 '12 at 11:00