| | | 1 | | using System.Reflection; |
| | | 2 | | using Elsa.Features.Services; |
| | | 3 | | using FastEndpoints; |
| | | 4 | | |
| | | 5 | | // ReSharper disable once CheckNamespace |
| | | 6 | | namespace Elsa.Extensions; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Provides extensions to <see cref="IModule"/>. |
| | | 10 | | /// </summary> |
| | | 11 | | public static class ModuleExtensions |
| | | 12 | | { |
| | 1 | 13 | | private static readonly object FastEndpointsAssembliesKey = new(); |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Registers the specified assembly for FastEndpoint assembly discovery. |
| | | 17 | | /// </summary> |
| | | 18 | | public static IModule AddFastEndpointsAssembly(this IModule module, Assembly assembly) |
| | | 19 | | { |
| | 6 | 20 | | var assemblies = module.Properties.GetOrAdd(FastEndpointsAssembliesKey, () => new HashSet<Assembly>()); |
| | 5 | 21 | | assemblies.Add(assembly); |
| | 5 | 22 | | return module; |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Registers the assembly for FastEndpoint assembly discovery using the specified marker type. |
| | | 27 | | /// </summary> |
| | 3 | 28 | | public static IModule AddFastEndpointsAssembly<T>(this IModule module) => module.AddFastEndpointsAssembly(typeof(T)) |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Registers the assembly for FastEndpoint assembly discovery using the specified marker type. |
| | | 32 | | /// </summary> |
| | 5 | 33 | | public static IModule AddFastEndpointsAssembly(this IModule module, Type markerType) => module.AddFastEndpointsAssem |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Returns all collected assemblies for discovery of endpoints. |
| | | 37 | | /// </summary> |
| | 1 | 38 | | public static IEnumerable<Assembly> GetFastEndpointsAssembliesFromModule(this IModule module) => module.Properties.G |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Adds FastEndpoints to the service container and registers all collected assemblies for discovery of endpoints. |
| | | 42 | | /// </summary> |
| | | 43 | | public static IModule AddFastEndpointsFromModule(this IModule module) |
| | | 44 | | { |
| | 1 | 45 | | var assemblies = module.GetFastEndpointsAssembliesFromModule().ToList(); |
| | | 46 | | |
| | 1 | 47 | | module.Services.AddFastEndpoints(options => |
| | 1 | 48 | | { |
| | 1 | 49 | | options.DisableAutoDiscovery = true; |
| | 1 | 50 | | options.Assemblies = assemblies; |
| | 2 | 51 | | }); |
| | | 52 | | |
| | 1 | 53 | | return module; |
| | | 54 | | } |
| | | 55 | | } |