1 namespace System.Web.Mvc {
3 using System.Web.Mvc.Async;
4 using System.Web.Routing;
6 public abstract class AsyncController : Controller, IAsyncManagerContainer, IAsyncController {
8 private static readonly object _executeTag = new object();
9 private static readonly object _executeCoreTag = new object();
11 private readonly AsyncManager _asyncManager = new AsyncManager();
13 public AsyncManager AsyncManager {
19 protected virtual IAsyncResult BeginExecute(RequestContext requestContext, AsyncCallback callback, object state) {
20 if (requestContext == null) {
21 throw new ArgumentNullException("requestContext");
24 VerifyExecuteCalledOnce();
25 Initialize(requestContext);
26 return AsyncResultWrapper.Begin(callback, state, BeginExecuteCore, EndExecuteCore, _executeTag);
29 protected virtual IAsyncResult BeginExecuteCore(AsyncCallback callback, object state) {
30 // If code in this method needs to be updated, please also check the ExecuteCore() method
31 // of Controller to see if that code also must be updated.
33 PossiblyLoadTempData();
35 string actionName = RouteData.GetRequiredString("action");
36 IActionInvoker invoker = ActionInvoker;
37 IAsyncActionInvoker asyncInvoker = invoker as IAsyncActionInvoker;
38 if (asyncInvoker != null) {
39 // asynchronous invocation
40 BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) {
41 return asyncInvoker.BeginInvokeAction(ControllerContext, actionName, asyncCallback, asyncState);
44 EndInvokeDelegate endDelegate = delegate(IAsyncResult asyncResult) {
45 if (!asyncInvoker.EndInvokeAction(asyncResult)) {
46 HandleUnknownAction(actionName);
50 return AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _executeCoreTag);
53 // synchronous invocation
54 Action action = () => {
55 if (!invoker.InvokeAction(ControllerContext, actionName)) {
56 HandleUnknownAction(actionName);
59 return AsyncResultWrapper.BeginSynchronous(callback, state, action, _executeCoreTag);
63 PossiblySaveTempData();
68 protected override IActionInvoker CreateActionInvoker() {
69 return new AsyncControllerActionInvoker();
72 protected virtual void EndExecute(IAsyncResult asyncResult) {
73 AsyncResultWrapper.End(asyncResult, _executeTag);
76 protected virtual void EndExecuteCore(IAsyncResult asyncResult) {
77 // If code in this method needs to be updated, please also check the ExecuteCore() method
78 // of Controller to see if that code also must be updated.
81 AsyncResultWrapper.End(asyncResult, _executeCoreTag);
84 PossiblySaveTempData();
88 #region IAsyncController Members
89 IAsyncResult IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, object state) {
90 return BeginExecute(requestContext, callback, state);
93 void IAsyncController.EndExecute(IAsyncResult asyncResult) {
94 EndExecute(asyncResult);