| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using System.Diagnostics.CodeAnalysis; |
| | | 3 | | using System.Reflection; |
| | | 4 | | |
| | | 5 | | namespace AsyncResponse; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Reflection helpers over <see cref="IAsyncResponsePayload"/> implementations. Durable channels |
| | | 9 | | /// use this to fail fast when a recovery-enabled flow's payload has not overridden |
| | | 10 | | /// <see cref="IAsyncResponsePayload.ShouldResumeOnRecovery"/> and would otherwise silently take the |
| | | 11 | | /// conservative default (never resume). |
| | | 12 | | /// </summary> |
| | | 13 | | public static class AsyncResponsePayloadReflection |
| | | 14 | | { |
| | 3 | 15 | | private static readonly ConcurrentDictionary<Type, bool> OverrideCache = new(); |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Returns <c>true</c> when <paramref name="payloadType"/> provides its own implementation of |
| | | 19 | | /// <see cref="IAsyncResponsePayload.ShouldResumeOnRecovery"/> rather than inheriting the |
| | | 20 | | /// interface's default. The result is cached per type. |
| | | 21 | | /// </summary> |
| | | 22 | | public static bool OverridesShouldResumeOnRecovery(Type payloadType) |
| | | 23 | | { |
| | 3 | 24 | | ArgumentNullException.ThrowIfNull(payloadType); |
| | | 25 | | |
| | 3 | 26 | | return OverrideCache.GetOrAdd(payloadType, DetectOverride); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | [UnconditionalSuppressMessage("Trimming", "IL2070", |
| | | 30 | | Justification = "Best-effort diagnostic: payload types reaching this check are statically referenced by the wait |
| | | 31 | | "registration that triggers it (typeof(T)), so their methods are preserved; when the runtime sti |
| | | 32 | | "cannot answer, the check fails open rather than failing a correct app.")] |
| | | 33 | | private static bool DetectOverride(Type type) |
| | | 34 | | { |
| | 3 | 35 | | if (type.IsInterface || !typeof(IAsyncResponsePayload).IsAssignableFrom(type)) |
| | 3 | 36 | | return false; |
| | | 37 | | |
| | | 38 | | // Implicit implementations (the overwhelmingly common shape) declare a public |
| | | 39 | | // ShouldResumeOnRecovery on the class itself — detectable without the interface map. |
| | 3 | 40 | | if (type.GetMethod(nameof(IAsyncResponsePayload.ShouldResumeOnRecovery), BindingFlags.Instance | BindingFlags.Pu |
| | 3 | 41 | | return true; |
| | | 42 | | |
| | | 43 | | try |
| | | 44 | | { |
| | 3 | 45 | | var map = type.GetInterfaceMap(typeof(IAsyncResponsePayload)); |
| | 3 | 46 | | var interfaceMethod = typeof(IAsyncResponsePayload).GetMethod(nameof(IAsyncResponsePayload.ShouldResumeOnRec |
| | 3 | 47 | | var index = Array.IndexOf(map.InterfaceMethods, interfaceMethod); |
| | | 48 | | |
| | | 49 | | // When the type does not implement the method, the interface map points the target |
| | | 50 | | // back at the interface's own default implementation. |
| | 3 | 51 | | return map.TargetMethods[index].DeclaringType != typeof(IAsyncResponsePayload); |
| | | 52 | | } |
| | 1 | 53 | | catch (NotSupportedException) |
| | | 54 | | { |
| | | 55 | | // Native AOT cannot compute the interface map for interfaces with default |
| | | 56 | | // implementations. This check exists to fail fast on a payload that silently |
| | | 57 | | // inherits the conservative default — when the runtime cannot answer, assume the |
| | | 58 | | // payload is fine instead of breaking a correct app (fail open). |
| | 1 | 59 | | return true; |
| | | 60 | | } |
| | 3 | 61 | | } |
| | | 62 | | } |