| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using System.Reflection; |
| | | 3 | | |
| | | 4 | | namespace AsyncResponse; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Opt-in extensibility for resolving the service and payload types named in persisted callbacks and |
| | | 8 | | /// recovery state. By default AsyncResponse resolves a type name against the assemblies loaded into |
| | | 9 | | /// the default <see cref="System.Runtime.Loader.AssemblyLoadContext"/> |
| | | 10 | | /// (<c>AppDomain.CurrentDomain.GetAssemblies()</c>). Apps that load callback targets or payload types |
| | | 11 | | /// into a <em>separate</em> <c>AssemblyLoadContext</c> — plugin hosts, dynamic-load scenarios — can |
| | | 12 | | /// register an extra resolver (or assembly) here so those types resolve too, instead of the recovery |
| | | 13 | | /// callback silently failing because the type was invisible to the default context. |
| | | 14 | | /// <para> |
| | | 15 | | /// This is process-wide and additive: registered resolvers are consulted only when the default scan |
| | | 16 | | /// does not find the type. Registering nothing preserves the default behavior exactly. |
| | | 17 | | /// </para> |
| | | 18 | | /// </summary> |
| | | 19 | | public static class AsyncResponseTypeResolution |
| | | 20 | | { |
| | 2 | 21 | | private static volatile Func<string, Type?>[] _resolvers = []; |
| | 2 | 22 | | private static readonly object _gate = new(); |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Registers a custom resolver consulted (after the default assembly scan) when resolving a |
| | | 26 | | /// persisted type name. The resolver returns the resolved <see cref="Type"/> or <c>null</c>. |
| | | 27 | | /// </summary> |
| | | 28 | | public static void RegisterResolver(Func<string, Type?> resolver) |
| | | 29 | | { |
| | 2 | 30 | | ArgumentNullException.ThrowIfNull(resolver); |
| | 2 | 31 | | lock (_gate) |
| | | 32 | | { |
| | 2 | 33 | | _resolvers = [.. _resolvers, resolver]; |
| | 2 | 34 | | } |
| | | 35 | | |
| | | 36 | | // A new resolver can turn previously-unresolvable names into hits; drop the negative cache. |
| | 2 | 37 | | ReflectionExtensions.InvalidateUnresolvableServiceTypes(); |
| | 2 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Registers an assembly (typically one loaded into a non-default <c>AssemblyLoadContext</c>) to |
| | | 42 | | /// be searched for persisted type names. |
| | | 43 | | /// </summary> |
| | | 44 | | [RequiresUnreferencedCode("Resolves persisted type names against the assembly by string; a trimmed app may have remo |
| | | 45 | | "those types. Plugin/dynamic-load scenarios are inherently incompatible with trimming the |
| | | 46 | | public static void RegisterAssembly(Assembly assembly) |
| | | 47 | | { |
| | 2 | 48 | | ArgumentNullException.ThrowIfNull(assembly); |
| | 2 | 49 | | RegisterResolver(name => assembly.GetType(name, throwOnError: false)); |
| | 2 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary>Consults the registered resolvers in order; returns the first non-null match, or <c>null</c>.</summary> |
| | | 53 | | internal static Type? Resolve(string fullName) |
| | | 54 | | { |
| | 2 | 55 | | foreach (var resolver in _resolvers) |
| | | 56 | | { |
| | | 57 | | try |
| | | 58 | | { |
| | 2 | 59 | | if (resolver(fullName) is { } type) |
| | 2 | 60 | | return type; |
| | 2 | 61 | | } |
| | 2 | 62 | | catch |
| | | 63 | | { |
| | | 64 | | // A misbehaving custom resolver must never break recovery resolution; skip to the next. |
| | 2 | 65 | | } |
| | | 66 | | } |
| | | 67 | | |
| | 2 | 68 | | return null; |
| | 2 | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary>Clears all registered resolvers. Test seam only.</summary> |
| | | 72 | | internal static void Reset() |
| | | 73 | | { |
| | 2 | 74 | | lock (_gate) |
| | | 75 | | { |
| | 2 | 76 | | _resolvers = []; |
| | 2 | 77 | | } |
| | | 78 | | |
| | 2 | 79 | | ReflectionExtensions.InvalidateUnresolvableServiceTypes(); |
| | 2 | 80 | | } |
| | | 81 | | } |