forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncControllerActionInvoker.cs
More file actions
443 lines (394 loc) · 22.9 KB
/
AsyncControllerActionInvoker.cs
File metadata and controls
443 lines (394 loc) · 22.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Security.Principal;
using System.Threading;
using System.Web.Mvc.Filters;
using System.Web.Mvc.Routing;
namespace System.Web.Mvc.Async
{
public class AsyncControllerActionInvoker : ControllerActionInvoker, IAsyncActionInvoker
{
private static readonly object _invokeActionTag = new object();
private static readonly object _invokeActionMethodTag = new object();
private static readonly object _invokeActionMethodWithFiltersTag = new object();
[SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling", Justification = "Refactoring to reduce coupling not currently justified.")]
public virtual IAsyncResult BeginInvokeAction(ControllerContext controllerContext, string actionName, AsyncCallback callback, object state)
{
if (controllerContext == null)
{
throw new ArgumentNullException("controllerContext");
}
Contract.Assert(controllerContext.RouteData != null);
if (String.IsNullOrEmpty(actionName) && !controllerContext.RouteData.HasDirectRouteMatch())
{
throw Error.ParameterCannotBeNullOrEmpty("actionName");
}
ControllerDescriptor controllerDescriptor = GetControllerDescriptor(controllerContext);
ActionDescriptor actionDescriptor = FindAction(controllerContext, controllerDescriptor, actionName);
if (actionDescriptor != null)
{
FilterInfo filterInfo = GetFilters(controllerContext, actionDescriptor);
Action continuation = null;
BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState)
{
try
{
AuthenticationContext authenticationContext = InvokeAuthenticationFilters(controllerContext,
filterInfo.AuthenticationFilters, actionDescriptor);
if (authenticationContext.Result != null)
{
// An authentication filter signaled that we should short-circuit the request. Let all
// authentication filters contribute to an action result (to combine authentication
// challenges). Then, run this action result.
AuthenticationChallengeContext challengeContext =
InvokeAuthenticationFiltersChallenge(controllerContext,
filterInfo.AuthenticationFilters, actionDescriptor, authenticationContext.Result);
continuation = () => InvokeActionResult(controllerContext,
challengeContext.Result ?? authenticationContext.Result);
}
else
{
AuthorizationContext authorizationContext = InvokeAuthorizationFilters(controllerContext, filterInfo.AuthorizationFilters, actionDescriptor);
if (authorizationContext.Result != null)
{
// An authorization filter signaled that we should short-circuit the request. Let all
// authentication filters contribute to an action result (to combine authentication
// challenges). Then, run this action result.
AuthenticationChallengeContext challengeContext =
InvokeAuthenticationFiltersChallenge(controllerContext,
filterInfo.AuthenticationFilters, actionDescriptor, authorizationContext.Result);
continuation = () => InvokeActionResult(controllerContext,
challengeContext.Result ?? authorizationContext.Result);
}
else
{
if (controllerContext.Controller.ValidateRequest)
{
ValidateRequest(controllerContext);
}
IDictionary<string, object> parameters = GetParameterValues(controllerContext, actionDescriptor);
IAsyncResult asyncResult = BeginInvokeActionMethodWithFilters(controllerContext, filterInfo.ActionFilters, actionDescriptor, parameters, asyncCallback, asyncState);
continuation = () =>
{
ActionExecutedContext postActionContext = EndInvokeActionMethodWithFilters(asyncResult);
// The action succeeded. Let all authentication filters contribute to an action
// result (to combine authentication challenges; some authentication filters need
// to do negotiation even on a successful result). Then, run this action result.
AuthenticationChallengeContext challengeContext =
InvokeAuthenticationFiltersChallenge(controllerContext,
filterInfo.AuthenticationFilters, actionDescriptor,
postActionContext.Result);
InvokeActionResultWithFilters(controllerContext, filterInfo.ResultFilters,
challengeContext.Result ?? postActionContext.Result);
};
return asyncResult;
}
}
}
catch (ThreadAbortException)
{
// This type of exception occurs as a result of Response.Redirect(), but we special-case so that
// the filters don't see this as an error.
throw;
}
catch (Exception ex)
{
// something blew up, so execute the exception filters
ExceptionContext exceptionContext = InvokeExceptionFilters(controllerContext, filterInfo.ExceptionFilters, ex);
if (!exceptionContext.ExceptionHandled)
{
throw;
}
continuation = () => InvokeActionResult(controllerContext, exceptionContext.Result);
}
return BeginInvokeAction_MakeSynchronousAsyncResult(asyncCallback, asyncState);
};
EndInvokeDelegate<bool> endDelegate = delegate(IAsyncResult asyncResult)
{
try
{
continuation();
}
catch (ThreadAbortException)
{
// This type of exception occurs as a result of Response.Redirect(), but we special-case so that
// the filters don't see this as an error.
throw;
}
catch (Exception ex)
{
// something blew up, so execute the exception filters
ExceptionContext exceptionContext = InvokeExceptionFilters(controllerContext, filterInfo.ExceptionFilters, ex);
if (!exceptionContext.ExceptionHandled)
{
throw;
}
InvokeActionResult(controllerContext, exceptionContext.Result);
}
return true;
};
return AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _invokeActionTag);
}
else
{
// Notify the controller that no action was found.
return BeginInvokeAction_ActionNotFound(callback, state);
}
}
private static IAsyncResult BeginInvokeAction_ActionNotFound(AsyncCallback callback, object state)
{
BeginInvokeDelegate beginDelegate = BeginInvokeAction_MakeSynchronousAsyncResult;
EndInvokeDelegate<bool> endDelegate = delegate(IAsyncResult asyncResult)
{
return false;
};
return AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _invokeActionTag);
}
private static IAsyncResult BeginInvokeAction_MakeSynchronousAsyncResult(AsyncCallback callback, object state)
{
SimpleAsyncResult asyncResult = new SimpleAsyncResult(state);
asyncResult.MarkCompleted(true /* completedSynchronously */, callback);
return asyncResult;
}
protected internal virtual IAsyncResult BeginInvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary<string, object> parameters, AsyncCallback callback, object state)
{
AsyncActionDescriptor asyncActionDescriptor = actionDescriptor as AsyncActionDescriptor;
if (asyncActionDescriptor != null)
{
return BeginInvokeAsynchronousActionMethod(controllerContext, asyncActionDescriptor, parameters, callback, state);
}
else
{
return BeginInvokeSynchronousActionMethod(controllerContext, actionDescriptor, parameters, callback, state);
}
}
protected internal virtual IAsyncResult BeginInvokeActionMethodWithFilters(ControllerContext controllerContext, IList<IActionFilter> filters, ActionDescriptor actionDescriptor, IDictionary<string, object> parameters, AsyncCallback callback, object state)
{
Func<ActionExecutedContext> endContinuation = null;
BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState)
{
AsyncInvocationWithFilters invocation = new AsyncInvocationWithFilters(this, controllerContext, actionDescriptor, filters, parameters, asyncCallback, asyncState);
const int StartingFilterIndex = 0;
endContinuation = invocation.InvokeActionMethodFilterAsynchronouslyRecursive(StartingFilterIndex);
if (invocation.InnerAsyncResult != null)
{
// we're just waiting for the inner result to complete
return invocation.InnerAsyncResult;
}
else
{
// something was short-circuited and the action was not called, so this was a synchronous operation
SimpleAsyncResult newAsyncResult = new SimpleAsyncResult(asyncState);
newAsyncResult.MarkCompleted(completedSynchronously: true, callback: asyncCallback);
return newAsyncResult;
}
};
EndInvokeDelegate<ActionExecutedContext> endDelegate = delegate(IAsyncResult asyncResult)
{
return endContinuation();
};
return AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _invokeActionMethodWithFiltersTag);
}
private IAsyncResult BeginInvokeAsynchronousActionMethod(ControllerContext controllerContext, AsyncActionDescriptor actionDescriptor, IDictionary<string, object> parameters, AsyncCallback callback, object state)
{
BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState)
{
return actionDescriptor.BeginExecute(controllerContext, parameters, asyncCallback, asyncState);
};
EndInvokeDelegate<ActionResult> endDelegate = delegate(IAsyncResult asyncResult)
{
object returnValue = actionDescriptor.EndExecute(asyncResult);
ActionResult result = CreateActionResult(controllerContext, actionDescriptor, returnValue);
return result;
};
return AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _invokeActionMethodTag);
}
private IAsyncResult BeginInvokeSynchronousActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary<string, object> parameters, AsyncCallback callback, object state)
{
// Frequently called so ensure delegate remains static and arguments do not allocate
EndInvokeDelegate<ActionInvocation, ActionResult> endInvokeFunc = (asyncResult, innerInvokeState) =>
{
return innerInvokeState.InvokeSynchronousActionMethod();
};
ActionInvocation endInvokeState = new ActionInvocation(this, controllerContext, actionDescriptor, parameters);
return AsyncResultWrapper.BeginSynchronous(callback, state, endInvokeFunc, endInvokeState, _invokeActionMethodTag);
}
public virtual bool EndInvokeAction(IAsyncResult asyncResult)
{
return AsyncResultWrapper.End<bool>(asyncResult, _invokeActionTag);
}
protected internal virtual ActionResult EndInvokeActionMethod(IAsyncResult asyncResult)
{
return AsyncResultWrapper.End<ActionResult>(asyncResult, _invokeActionMethodTag);
}
protected internal virtual ActionExecutedContext EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)
{
return AsyncResultWrapper.End<ActionExecutedContext>(asyncResult, _invokeActionMethodWithFiltersTag);
}
protected override ControllerDescriptor GetControllerDescriptor(ControllerContext controllerContext)
{
// Frequently called, so ensure delegate is static
Type controllerType = controllerContext.Controller.GetType();
ControllerDescriptor controllerDescriptor = DescriptorCache.GetDescriptor(
controllerType: controllerType,
creator: ReflectedAsyncControllerDescriptor.DefaultDescriptorFactory,
state: controllerType);
return controllerDescriptor;
}
// Keep as value type to avoid per-call allocation
private struct ActionInvocation
{
private readonly AsyncControllerActionInvoker _invoker;
private readonly ControllerContext _controllerContext;
private readonly ActionDescriptor _actionDescriptor;
private readonly IDictionary<string, object> _parameters;
internal ActionInvocation(AsyncControllerActionInvoker invoker, ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary<string, object> parameters)
{
Contract.Assert(invoker != null);
Contract.Assert(controllerContext != null);
Contract.Assert(actionDescriptor != null);
Contract.Assert(parameters != null);
_invoker = invoker;
_controllerContext = controllerContext;
_actionDescriptor = actionDescriptor;
_parameters = parameters;
}
internal ActionResult InvokeSynchronousActionMethod()
{
return _invoker.InvokeActionMethod(_controllerContext, _actionDescriptor, _parameters);
}
}
// Large and passed to many function calls, so keep as a reference type to minimize copying
private class AsyncInvocationWithFilters
{
private readonly AsyncControllerActionInvoker _invoker;
private readonly ControllerContext _controllerContext;
private readonly ActionDescriptor _actionDescriptor;
private readonly IList<IActionFilter> _filters;
private readonly IDictionary<string, object> _parameters;
private readonly AsyncCallback _asyncCallback;
private readonly object _asyncState;
private readonly int _filterCount;
private readonly ActionExecutingContext _preContext;
internal IAsyncResult InnerAsyncResult;
internal AsyncInvocationWithFilters(AsyncControllerActionInvoker invoker, ControllerContext controllerContext, ActionDescriptor actionDescriptor, IList<IActionFilter> filters, IDictionary<string, object> parameters, AsyncCallback asyncCallback, object asyncState)
{
Contract.Assert(invoker != null);
Contract.Assert(controllerContext != null);
Contract.Assert(actionDescriptor != null);
Contract.Assert(filters != null);
Contract.Assert(parameters != null);
_invoker = invoker;
_controllerContext = controllerContext;
_actionDescriptor = actionDescriptor;
_filters = filters;
_parameters = parameters;
_asyncCallback = asyncCallback;
_asyncState = asyncState;
_preContext = new ActionExecutingContext(controllerContext, actionDescriptor, parameters);
// For IList<T> it is faster to cache the count
_filterCount = _filters.Count;
}
internal Func<ActionExecutedContext> InvokeActionMethodFilterAsynchronouslyRecursive(int filterIndex)
{
// Performance-sensitive
// For compatability, the following behavior must be maintained
// The OnActionExecuting events must fire in forward order
// The Begin and End events must fire
// The OnActionExecuted events must fire in reverse order
// Earlier filters can process the results and exceptions from the handling of later filters
// This is achieved by calling recursively and moving through the filter list forwards
// If there are no more filters to recurse over, create the main result
if (filterIndex > _filterCount - 1)
{
InnerAsyncResult = _invoker.BeginInvokeActionMethod(_controllerContext, _actionDescriptor, _parameters, _asyncCallback, _asyncState);
return () =>
new ActionExecutedContext(_controllerContext, _actionDescriptor, canceled: false, exception: null)
{
Result = _invoker.EndInvokeActionMethod(InnerAsyncResult)
};
}
// Otherwise process the filters recursively
IActionFilter filter = _filters[filterIndex];
ActionExecutingContext preContext = _preContext;
filter.OnActionExecuting(preContext);
if (preContext.Result != null)
{
ActionExecutedContext shortCircuitedPostContext = new ActionExecutedContext(preContext, preContext.ActionDescriptor, canceled: true, exception: null)
{
Result = preContext.Result
};
return () => shortCircuitedPostContext;
}
// There is a nested try / catch block here that contains much the same logic as the outer block.
// Since an exception can occur on either side of the asynchronous invocation, we need guards on
// on both sides. In the code below, the second side is represented by the nested delegate. This
// is really just a parallel of the synchronous ControllerActionInvoker.InvokeActionMethodFilter()
// method.
try
{
// Use the filters in forward direction
int nextFilterIndex = filterIndex + 1;
Func<ActionExecutedContext> continuation = InvokeActionMethodFilterAsynchronouslyRecursive(nextFilterIndex);
// add our own continuation, then return the new function
return () =>
{
ActionExecutedContext postContext;
bool wasError = true;
try
{
postContext = continuation();
wasError = false;
}
catch (ThreadAbortException)
{
// This type of exception occurs as a result of Response.Redirect(), but we special-case so that
// the filters don't see this as an error.
postContext = new ActionExecutedContext(preContext, preContext.ActionDescriptor, canceled: false, exception: null);
filter.OnActionExecuted(postContext);
throw;
}
catch (Exception ex)
{
postContext = new ActionExecutedContext(preContext, preContext.ActionDescriptor, canceled: false, exception: ex);
filter.OnActionExecuted(postContext);
if (!postContext.ExceptionHandled)
{
throw;
}
}
if (!wasError)
{
filter.OnActionExecuted(postContext);
}
return postContext;
};
}
catch (ThreadAbortException)
{
// This type of exception occurs as a result of Response.Redirect(), but we special-case so that
// the filters don't see this as an error.
ActionExecutedContext postContext = new ActionExecutedContext(preContext, preContext.ActionDescriptor, canceled: false, exception: null);
filter.OnActionExecuted(postContext);
throw;
}
catch (Exception ex)
{
ActionExecutedContext postContext = new ActionExecutedContext(preContext, preContext.ActionDescriptor, canceled: false, exception: ex);
filter.OnActionExecuted(postContext);
if (postContext.ExceptionHandled)
{
return () => postContext;
}
else
{
throw;
}
}
}
}
}
}