2024-08-13 23:08:00 +00:00
|
|
|
|
using Medusa.Core.Handlers;
|
|
|
|
|
using Medusa.Core.Services;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
|
|
namespace Medusa.Core.Extensions
|
2024-08-13 07:55:18 +00:00
|
|
|
|
{
|
|
|
|
|
public static class ApplicationBuilderExtensions
|
|
|
|
|
{
|
|
|
|
|
public static IApplicationBuilder UseHandlers(this IApplicationBuilder app)
|
|
|
|
|
{
|
2024-08-13 23:08:00 +00:00
|
|
|
|
var handlerService = app.ApplicationServices.GetRequiredService<IHandlerService>();
|
|
|
|
|
|
|
|
|
|
var assembly = Assembly.GetEntryAssembly() ?? throw new InvalidOperationException("Could not find entry assembly.");
|
2024-08-19 07:26:41 +00:00
|
|
|
|
var types = assembly.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IHandler)) ||
|
|
|
|
|
(t.IsSubclassOf(typeof(Handler)) && !t.IsAbstract));
|
2024-08-13 23:08:00 +00:00
|
|
|
|
|
|
|
|
|
handlerService.Handlers.AddRange(types);
|
|
|
|
|
|
2024-08-13 07:55:18 +00:00
|
|
|
|
return app;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|