diff --git a/Source/Shared/Features/Messaging/Registrator.cs b/Source/Shared/Features/Messaging/Registrator.cs index c0025ed8..8328ac09 100644 --- a/Source/Shared/Features/Messaging/Registrator.cs +++ b/Source/Shared/Features/Messaging/Registrator.cs @@ -10,13 +10,20 @@ namespace Shared.Features.Messaging { public static class Registrator { - public static IServiceCollection AddMessaging(this IServiceCollection services, Assembly[] assemblies) + public static IServiceCollection AddMessaging(this IServiceCollection services) { services.TryAddScoped(); services.TryAddScoped(); services.TryAddScoped(); services.TryAddScoped(); + return services; + } + + public static IServiceCollection AddMessagingForModule(this IServiceCollection services, Type moduleType) + { + var assemblies = new Assembly[] { moduleType.Assembly }; + // INFO: Using https://www.nuget.org/packages/Scrutor for registering all Query and Command handlers by convention services.Scan(selector => { @@ -37,6 +44,14 @@ public static IServiceCollection AddMessaging(this IServiceCollection services, }) .AsImplementedInterfaces() .WithScopedLifetime(); + + selector.FromAssemblies(assemblies) + .AddClasses(filter => + { + filter.AssignableTo(typeof(ICommandHandler<,>)); + }) + .AsImplementedInterfaces() + .WithScopedLifetime(); }); services.Scan(selector => { @@ -58,6 +73,7 @@ public static IServiceCollection AddMessaging(this IServiceCollection services, .AsImplementedInterfaces() .WithScopedLifetime(); }); + return services; } } diff --git a/Source/Shared/Features/Modules/Registrator.cs b/Source/Shared/Features/Modules/Registrator.cs index d4962cc5..d21b9727 100644 --- a/Source/Shared/Features/Modules/Registrator.cs +++ b/Source/Shared/Features/Modules/Registrator.cs @@ -23,7 +23,7 @@ public static IServiceCollection AddModule(this IServiceCollecti } public static IServiceCollection AddModule(this IServiceCollection services, IConfiguration config = null) - where TModule : IModule + where TModule : class, IModule where TModuleStartup : IModuleStartup { // Register assembly in MVC so it can find controllers of the module @@ -33,34 +33,23 @@ public static IServiceCollection AddModule(this IServic var moduleStartup = (IModuleStartup)Activator.CreateInstance(typeof(TModuleStartup)); moduleStartup.ConfigureServices(services, config); - var module = (IModule)ActivatorUtilities.CreateInstance(services.BuildServiceProvider()); + var module = ActivatorUtilities.CreateInstance(services.BuildServiceProvider()); services.AddScoped(sp => module); + services.AddMessagingForModule(module.GetType()); return services; } - public static void AddModules(this IServiceCollection services) - { - var serviceProvider = services.BuildServiceProvider(); - - var startupModules = serviceProvider.GetRequiredService>(); - - services.AddMessaging(startupModules.Where(sm => sm.FeaturesAssembly is not null).Select(sm => sm.FeaturesAssembly).ToArray()); - } - public static IApplicationBuilder UseModulesMiddleware(this IApplicationBuilder app, IHostEnvironment env) { - app.Use(async (context, next) => - { - foreach (var module in context.RequestServices.GetRequiredService>()) - { - module.Configure(app, env); - } + using var scope = app.ApplicationServices.CreateAsyncScope(); - await next(); - }); + foreach (var moduleStartup in scope.ServiceProvider.GetRequiredService>()) + { + moduleStartup.Configure(app, env); + } return app; - } + } } } diff --git a/Source/Shared/Features/Registrator.cs b/Source/Shared/Features/Registrator.cs index 35038472..a5dda19f 100644 --- a/Source/Shared/Features/Registrator.cs +++ b/Source/Shared/Features/Registrator.cs @@ -16,9 +16,8 @@ public static IServiceCollection AddSharedFeatures(this IServiceCollection servi { var serviceProvider = services.BuildServiceProvider(); var configuration = serviceProvider.GetRequiredService(); - var startupModules = serviceProvider.GetRequiredService>(); - services.AddMessaging(startupModules.Where(sm => sm.FeaturesAssembly is not null).Select(x => x.FeaturesAssembly).ToArray()); + services.AddMessaging(); services.AddEFCore(configuration); services.AddEmailSender(configuration); diff --git a/Source/Web/Server/Startup.cs b/Source/Web/Server/Startup.cs index 0806f173..c9fb8a27 100644 --- a/Source/Web/Server/Startup.cs +++ b/Source/Web/Server/Startup.cs @@ -41,7 +41,6 @@ public void ConfigureServices(IServiceCollection services) services.AddModule(Configuration); services.AddModule(Configuration); services.AddModule(Configuration); - services.AddModules(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.