IAsyncResult IController.BeginExecute(ActionInvocation action, AsyncCallback callback, object asyncState) { if (action == null) { throw new ArgumentNullException("action"); } if (callback == null) { throw new ArgumentNullException("callback"); } if (_asyncResult != null) { throw new InvalidOperationException(); } _action = action; ActionDescriptor actionDescriptor = ControllerDescriptor.GetAction(this, action.ActionName); if (actionDescriptor == null) { throw new InvalidOperationException(); } if (actionDescriptor.IsAsync) { _asyncResult = new ActionAsyncResult(this, callback, asyncState); try { Task<ActionResult> task = (Task<ActionResult>)actionDescriptor.Invoke(this, action); task.Completed += OnActionCompleted; } catch (Exception e) { _asyncResult = new ActionAsyncResult(new ErrorActionResult(e), asyncState); callback(_asyncResult); } } else { ActionResult result; try { result = (ActionResult)actionDescriptor.Invoke(this, action); } catch (Exception e) { result = new ErrorActionResult(e); } _asyncResult = new ActionAsyncResult(result, asyncState); callback(_asyncResult); } return _asyncResult; }