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?

4
  • this might be related: stackoverflow.com/questions/9977309/… Commented May 9, 2012 at 22:34
  • when you say it debugs with no errors, does a breakpoint on the Content() line for Test2 get hit? If so, does adding the Yield like the related SO thread suggests help any? Commented May 9, 2012 at 22:36
  • Yes the Content() line gets hit and the method actually completes. It gets stuck after method returns to MVC engine. Commented May 10, 2012 at 0:24
  • 1
    <code>Task.Yield()</code> did not help. Commented May 10, 2012 at 0:24

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.