Path: redb-route/redb.Route.Controllers/ActionInvoker.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Controllers/ActionInvoker.cs ---- using System.Reflection; using System.Runtime.ExceptionServices; namespace redb.Route.Controllers; /// /// Invokes a controller action and turns what it returned into the value the reply carries. Every dispatcher and /// the direct-invoke DSL overloads go through here, so an action behaves the same behind every transport. /// internal static class ActionInvoker { /// /// Calls on , awaits it when it is asynchronous, and /// returns its result: null for void, and . /// /// The shape is read from the method's DECLARED return type, as ASP.NET Core's ObjectMethodExecutor /// does. The run-time type cannot tell: an async Task method returns a Task<VoidTaskResult>, /// whose runtime-internal result then reached the reply as a body; and a is a /// struct that is not a at all, so it reached the reply unawaited. /// /// /// An exception the action throws synchronously arrives wrapped in by /// ; exactly that one wrapper is removed and the action's own /// exception is rethrown with its stack trace. An asynchronous action's exception comes out of the awaited /// task as it is. Nothing below that level is unwrapped. /// /// internal static async Task InvokeAsync(MethodInfo method, object controller, object?[] arguments) { object? returned; try { returned = method.Invoke(controller, arguments); } catch (TargetInvocationException tie) when (tie.InnerException is not null) { ExceptionDispatchInfo.Capture(tie.InnerException).Throw(); throw; // unreachable: Throw() never returns } var declared = method.ReturnType; if (declared == typeof(Task)) { await ((Task)returned!).ConfigureAwait(false); return null; } if (declared == typeof(ValueTask)) { await ((ValueTask)returned!).ConfigureAwait(false); return null; } if (declared.IsGenericType) { var definition = declared.GetGenericTypeDefinition(); if (definition == typeof(Task<>)) { var task = (Task)returned!; await task.ConfigureAwait(false); return declared.GetProperty(nameof(Task.Result))!.GetValue(task); } if (definition == typeof(ValueTask<>)) { var task = (Task)declared.GetMethod(nameof(ValueTask.AsTask))!.Invoke(returned, null)!; await task.ConfigureAwait(false); return typeof(Task<>).MakeGenericType(declared.GetGenericArguments()) .GetProperty(nameof(Task.Result))!.GetValue(task); } } return returned; } }