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