From 8b1ee39fe29fd12c8042d22f9fd1a969b5fcbc16 Mon Sep 17 00:00:00 2001 From: Jimmy Bogard Date: Thu, 16 Nov 2023 16:08:24 +0100 Subject: [PATCH] Adding configuration to auto-register request processors --- .../MediatrServiceConfiguration.cs | 5 ++++ src/MediatR/Registration/ServiceRegistrar.cs | 14 ++++++++++- .../MicrosoftExtensionsDI/PipelineTests.cs | 23 +++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/MediatR/MicrosoftExtensionsDI/MediatrServiceConfiguration.cs b/src/MediatR/MicrosoftExtensionsDI/MediatrServiceConfiguration.cs index 539d0b2c..53712c42 100644 --- a/src/MediatR/MicrosoftExtensionsDI/MediatrServiceConfiguration.cs +++ b/src/MediatR/MicrosoftExtensionsDI/MediatrServiceConfiguration.cs @@ -64,6 +64,11 @@ public class MediatRServiceConfiguration /// public List RequestPostProcessorsToRegister { get; } = new(); + /// + /// Automatically register processors during assembly scanning + /// + public bool AutoRegisterRequestProcessors { get; set; } + /// /// Register various handlers from assembly containing given type /// diff --git a/src/MediatR/Registration/ServiceRegistrar.cs b/src/MediatR/Registration/ServiceRegistrar.cs index 721312eb..28ba295d 100644 --- a/src/MediatR/Registration/ServiceRegistrar.cs +++ b/src/MediatR/Registration/ServiceRegistrar.cs @@ -21,13 +21,25 @@ public static void AddMediatRClasses(IServiceCollection services, MediatRService ConnectImplementationsToTypesClosing(typeof(IRequestExceptionHandler<,,>), services, assembliesToScan, true, configuration); ConnectImplementationsToTypesClosing(typeof(IRequestExceptionAction<,>), services, assembliesToScan, true, configuration); - var multiOpenInterfaces = new[] + if (configuration.AutoRegisterRequestProcessors) + { + ConnectImplementationsToTypesClosing(typeof(IRequestPreProcessor<>), services, assembliesToScan, false, configuration); + ConnectImplementationsToTypesClosing(typeof(IRequestPostProcessor<,>), services, assembliesToScan, false, configuration); + } + + var multiOpenInterfaces = new List { typeof(INotificationHandler<>), typeof(IRequestExceptionHandler<,,>), typeof(IRequestExceptionAction<,>) }; + if (configuration.AutoRegisterRequestProcessors) + { + multiOpenInterfaces.Add(typeof(IRequestPreProcessor<>)); + multiOpenInterfaces.Add(typeof(IRequestPostProcessor<,>)); + } + foreach (var multiOpenInterface in multiOpenInterfaces) { var arity = multiOpenInterface.GetGenericArguments().Length; diff --git a/test/MediatR.Tests/MicrosoftExtensionsDI/PipelineTests.cs b/test/MediatR.Tests/MicrosoftExtensionsDI/PipelineTests.cs index 3722a378..6d5276a1 100644 --- a/test/MediatR.Tests/MicrosoftExtensionsDI/PipelineTests.cs +++ b/test/MediatR.Tests/MicrosoftExtensionsDI/PipelineTests.cs @@ -846,6 +846,29 @@ public void Should_handle_open_behaviors_registration_from_a_single_type() }); } + [Fact] + public void Should_auto_register_processors_when_configured() + { + var cfg = new MediatRServiceConfiguration + { + AutoRegisterRequestProcessors = true + }; + + var output = new Logger(); + IServiceCollection services = new ServiceCollection(); + services.AddSingleton(output); + + cfg.RegisterServicesFromAssemblyContaining(); + + services.AddMediatR(cfg); + + var provider = services.BuildServiceProvider(); + + provider.GetServices(typeof(IRequestPreProcessor)).Count().ShouldBeGreaterThan(0); + provider.GetServices(typeof(IRequestPostProcessor)).Count().ShouldBeGreaterThan(0); + } + + public sealed record FooRequest : IRequest; public interface IBlogger