示例#1
0
 private static void EnsureActionResultNotNull(ActionMethodMetadata metadata, IActionResult actionResult)
 {
     if (actionResult == null)
     {
         throw new InvalidOperationException(
                   $"Cannot return null from an action method with a return type of '{metadata.MethodReturnType}'.");
     }
 }
示例#2
0
            public override ReturnTask Execute(ObjectMethodExecutor executor, object controller,
                                               object[] arguments, ActionMethodMetadata metadata)
            {
                executor.Execute(controller, arguments);

#if NETSTANDARD
                return(Task.FromResult <IActionResult>(new OkResult()));
#else
                return(new ValueTask <IActionResult>(new OkResult()));
#endif
            }
示例#3
0
            public override async ReturnTask Execute(ObjectMethodExecutor executor, object controller,
                                                     object[] arguments, ActionMethodMetadata metadata)
            {
                // Async method returning Task<IActionResult>
                // Avoid extra allocations by calling Execute rather than ExecuteAsync and casting to Task<IActionResult>.
                var returnValue  = executor.Execute(controller, arguments);
                var actionResult = await(Task <IActionResult>) returnValue;

                EnsureActionResultNotNull(metadata, actionResult);

                return(actionResult);
            }
示例#4
0
        /// <summary>
        ///     Get the correct executer for a given <see cref="ActionMethodMetadata" />
        /// </summary>
        /// <param name="metadata">The metadata of the method</param>
        /// <returns>Return the correct executer for the method signature</returns>
        public static ActionMethodExecutor GetExecutor(ActionMethodMetadata metadata)
        {
            for (var i = 0; i < Executors.Length; i++)
            {
                if (Executors[i].CanExecute(metadata))
                {
                    return(Executors[i]);
                }
            }

            Debug.Fail("Should not get here");
            throw new Exception();
        }
示例#5
0
            public override ReturnTask Execute(ObjectMethodExecutor executor, object controller,
                                               object[] arguments, ActionMethodMetadata metadata)
            {
                var actionResult = (IActionResult)executor.Execute(controller, arguments);

                EnsureActionResultNotNull(metadata, actionResult);

#if NETSTANDARD
                return(Task.FromResult(actionResult));
#else
                return(new ValueTask <IActionResult>(actionResult));
#endif
            }
示例#6
0
            public override async ReturnTask Execute(ObjectMethodExecutor executor, object controller, object[] arguments,
                                                     ActionMethodMetadata metadata)
            {
                // Async method returning awaitable-of-IActionResult (e.g., Task<ViewResult>)
                // We have to use ExecuteAsync because we don't know the awaitable's type at compile time.
                var   task = (Task)executor.Execute(controller, arguments);
                await task;

                var resultProperty = typeof(Task <>).MakeGenericType(metadata.AsyncResultType).GetProperty("Result");
                var actionResult   = (IActionResult)resultProperty.GetValue(task);

                EnsureActionResultNotNull(metadata, actionResult);
                return(actionResult);
            }
示例#7
0
            public override ReturnTask Execute(ObjectMethodExecutor executor, object controller, object[] arguments,
                                               ActionMethodMetadata metadata)
            {
                // Sync method returning arbitrary object
                var returnValue  = executor.Execute(controller, arguments);
                var actionResult = new ObjectResult(returnValue)
                {
                    DeclaredType = metadata.MethodReturnType
                };

#if NETSTANDARD
                return(Task.FromResult <IActionResult>(actionResult));
#else
                return(new ValueTask <IActionResult>(actionResult));
#endif
            }
示例#8
0
            public override async ReturnTask Execute(ObjectMethodExecutor executor, object controller,
                                                     object[] arguments, ActionMethodMetadata metadata)
            {
                // Async method returning awaitable-of-nonvoid
                var   task = (Task)executor.Execute(controller, arguments);
                await task;

                var resultProperty = typeof(Task <>).MakeGenericType(metadata.AsyncResultType).GetProperty("Result");
                var result         = resultProperty.GetValue(task);

                var actionResult = new ObjectResult(result)
                {
                    DeclaredType = metadata.AsyncResultType
                };

                return(actionResult);
            }
示例#9
0
        /// <summary>
        /// Initialize a new instance of <see cref="ActionInvoker"/>
        /// </summary>
        /// <param name="controllerType">The controller type</param>
        /// <param name="routeMethod">The action method that should be executed</param>
        public ActionInvoker(Type controllerType, MethodInfo routeMethod)
        {
            Metadata = new ActionMethodMetadata(controllerType, routeMethod);

            ObjectFactory         = ActivatorUtilities.CreateFactory(controllerType, new Type[0]);
            _objectMethodExecutor = new ObjectMethodExecutor(routeMethod);
            _executor             = ActionMethodExecutor.GetExecutor(Metadata);

            var parameters = new List <ParameterDescriptor>();

            foreach (var parameter in routeMethod.GetParameters())
            {
                var methodParam = new ParameterDescriptor
                {
                    Name          = parameter.Name,
                    ParameterType = parameter.ParameterType,
                    BindingInfo   = GetBindingInfo(parameter)
                };
                parameters.Add(methodParam);
            }

            Parameters = parameters.ToImmutableList();
        }
示例#10
0
 protected override bool CanExecute(ActionMethodMetadata metadata) =>
 metadata.IsAsync && typeof(IActionResult).IsAssignableFrom(metadata.AsyncResultType);
示例#11
0
 protected override bool CanExecute(ActionMethodMetadata metadata)
 {
     return(!metadata.IsAsync && metadata.MethodReturnType == typeof(void));
 }
示例#12
0
 protected override bool CanExecute(ActionMethodMetadata metadata) => !metadata.IsAsync;
示例#13
0
 public override async ReturnTask Execute(ObjectMethodExecutor executor, object controller,
                                          object[] arguments, ActionMethodMetadata metadata)
 {
     await(Task) executor.Execute(controller, arguments);
     return(new EmptyResult());
 }
示例#14
0
 protected override bool CanExecute(ActionMethodMetadata metadata)
 {
     return(typeof(Task <IActionResult>).IsAssignableFrom(metadata.MethodReturnType));
 }
示例#15
0
 public abstract ReturnTask Execute(ObjectMethodExecutor executor, object controller,
                                    object[] arguments, ActionMethodMetadata metadata);
示例#16
0
 protected abstract bool CanExecute(ActionMethodMetadata metadata);
示例#17
0
 protected override bool CanExecute(ActionMethodMetadata metadata)
 {
     return(metadata.MethodReturnType == typeof(Task));
 }
示例#18
0
 protected override bool CanExecute(ActionMethodMetadata metadata) => true;
示例#19
0
 protected override bool CanExecute(ActionMethodMetadata methodMetadata)
 {
     return(!methodMetadata.IsAsync &&
            typeof(IActionResult).IsAssignableFrom(methodMetadata.MethodReturnType));
 }