Asp.net mvc 4 asynchronous controller hangs on one TaskAsync method

I am using Visual Studio 2011 Beta with 4.5 Beta. There seems to be an error with ASP.Net MVC 4, where if the method returns no "TaskAsync" task, it hangs with 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 great. Test2 freezes after method returns. I can debug code without errors.

Does anyone know of a bug / workaround for this?

+1
c # asp.net-mvc-4 async-await
source share
1 answer

Known issue with beta version of MVC 4.

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. (Remember to wait!)

+4
source share

All Articles