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}'."); } }
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 }
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); }
/// <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(); }
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 }
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); }
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 }
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); }
/// <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(); }
protected override bool CanExecute(ActionMethodMetadata metadata) => metadata.IsAsync && typeof(IActionResult).IsAssignableFrom(metadata.AsyncResultType);
protected override bool CanExecute(ActionMethodMetadata metadata) { return(!metadata.IsAsync && metadata.MethodReturnType == typeof(void)); }
protected override bool CanExecute(ActionMethodMetadata metadata) => !metadata.IsAsync;
public override async ReturnTask Execute(ObjectMethodExecutor executor, object controller, object[] arguments, ActionMethodMetadata metadata) { await(Task) executor.Execute(controller, arguments); return(new EmptyResult()); }
protected override bool CanExecute(ActionMethodMetadata metadata) { return(typeof(Task <IActionResult>).IsAssignableFrom(metadata.MethodReturnType)); }
public abstract ReturnTask Execute(ObjectMethodExecutor executor, object controller, object[] arguments, ActionMethodMetadata metadata);
protected abstract bool CanExecute(ActionMethodMetadata metadata);
protected override bool CanExecute(ActionMethodMetadata metadata) { return(metadata.MethodReturnType == typeof(Task)); }
protected override bool CanExecute(ActionMethodMetadata metadata) => true;
protected override bool CanExecute(ActionMethodMetadata methodMetadata) { return(!methodMetadata.IsAsync && typeof(IActionResult).IsAssignableFrom(methodMetadata.MethodReturnType)); }