18

Closed

Asynchronous child actions not supported

description

Attempting to use a child action that's marked as "async" in its method declaration results in the exception "HttpServerUtility.Execute blocked while waiting for an asynchronous operation to complete.".

Currently MVC 4 does not support asynchronous child actions, nor does it provide any graceful way of handling the request synchronously other than having to write a synchronous version of each action.

So far I've had to resort to hacks (http://stackoverflow.com/questions/13166180/force-synchronous-execution-of-asynchronous-action-in-asp-net-mvc-4/13210277#13210277) in order to force a blocking call.

A better solution might be to create a custom MvcHandler implementation that does this although MVC does not exactly make this easy with so many of the classes used in the current child action pipeline being internal/sealed.

Of course the ideal solution would be to have async child actions supported, but it would be nice to have a clean workaround until this is done.

There is also a request open on Uservoice for this, please vote. http://aspnet.uservoice.com/forums/41201-asp-net-mvc/suggestions/3233329-support-asynchronous-child-actions.

Code to reproduce:
public class HomeController : Controller
{
    //
    // GET: /Home/

    public ActionResult Index()
    {
        return View();
    }

    [ChildActionOnly]
    public async Task<ActionResult> Widget()
    {
        await Task.Delay(1000);
        return Content("widget");
    }

}
View:

@{
ViewBag.Title = "Index";
}
<h2>Index</h2> @Html.Action("widget")
Closed May 16 at 5:43 PM by yishaigalatzer

comments

yishaigalatzer wrote May 6, 2013 at 7:06 PM

Thanks for reporting this issue. Unfortunately child actions cannot be run asynchronously, furthermore calling asynchronous methods from a synchronous child action is not supported. There are several issues with the simplistic solution of calling the .Result on the method, as it can result in deadlocks/data corruption/or random exceptions.

The recommended approach is avoid using async code in child actions.

We are kicking around a few ideas, and if/when we come up with useful improvements we will circle back here.

tjrobinson wrote Sep 17, 2013 at 1:04 PM

"There are several issues with the simplistic solution of calling the .Result on the method, as it can result in deadlocks/data corruption/or random exceptions."

Can you be any more specific? What about if the child action has no choice but to call something down in a library that is async only. At what point is it safe to call the asynchronous method synchronously by wrapping it in .Result or similar?

yishaigalatzer wrote May 16 at 5:43 PM

In ASP.NET vNext MVC we added support for an equivalent of asynchronous child actions.

The new feature is named ViewComponents and it's completed.

To get started you can go to https://github.com/aspnet/home#getting-started

And specific use pattern is currently available in the MVC sample (not being specific here as the sample is in constant flux) https://github.com/aspnet/Mvc/tree/dev/samples