Medusa-CS/Medusa.Core/Extensions/ApplicationBuilderExtensions.cs

23 lines
765 B
C#

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<IHandlerService>();
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;
}
}
}