controllerServices.GetActionInvoker().InvokeActionAsync(actionContext, cancellationToken);
在DefaultServices中有如下代码 SetSingle<IHttpActionInvoker>(new ApiControllerActionInvoker());所以我们知道实际调用Action是在ApiControllerActionInvoker的InvokeActionAsync方法。ApiControllerActionInvoker的InvokeActionAsync方法主要就一句
return actionDescriptor.ExecuteAsync(controllerContext, actionContext.ActionArguments, cancellationToken)
.Then(value => actionDescriptor.ResultConverter.Convert(controllerContext, value), cancellationToken);
这里的actionContext.ActionArguments是一个字典数据,key是参数名称,value参数值。
我们知道actionDescriptor这里是一个ReflectedHttpActionDescriptor实例,其ExecuteAsync方法主要代码实现如下:
public override Task<object> ExecuteAsync(HttpControllerContext controllerContext, IDictionary<string, object> arguments, CancellationToken cancellationToken)
{
return TaskHelpers.RunSynchronously(() =>
{
object[] argumentValues = PrepareParameters(arguments, controllerContext);
return _actionExecutor.Value.Execute(controllerContext.Controller, argumentValues);
}, cancellationToken);
}
其中PrepareParameters方法主要是取出Action方法所需参数的值,并且还要做一些检查,PrepareParameters实现如下:
private object[] PrepareParameters(IDictionary<string, object> parameters, HttpControllerContext controllerContext)
{
// This is on a hotpath, so a quick check to