using Medusa.Core.Handlers; using Medusa.Core.Services; using System.Reflection; namespace Medusa.Core.Extensions { public static class ApplicationBuilderExtensions { public static IApplicationBuilder UseHandlers(this IApplicationBuilder app) { var handlerService = app.ApplicationServices.GetRequiredService(); var assembly = Assembly.GetEntryAssembly() ?? throw new InvalidOperationException("Could not find entry assembly."); var types = assembly.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IHandler)) || (t.IsSubclassOf(typeof(Handler)) && !t.IsAbstract)); handlerService.Handlers.AddRange(types); return app; } } }