user896993
user896993

Reputation: 1351

asp.net mvc 4 async controller hanging on none TaskAsync methods

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?

Upvotes: 1

Views: 2489

Answers (1)

Levi
Levi

Reputation: 32818

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!)

Upvotes: 4

Related Questions