From cccd3782a14c956db1920e153295aa4271511025 Mon Sep 17 00:00:00 2001 From: Henry Ing-Simmons Date: Fri, 29 Dec 2023 13:40:53 +0000 Subject: [PATCH] Fix AutoRegisterRequestProcessors to include all implementations --- src/MediatR/Registration/ServiceRegistrar.cs | 4 ++-- .../MicrosoftExtensionsDI/PipelineTests.cs | 13 ++++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/MediatR/Registration/ServiceRegistrar.cs b/src/MediatR/Registration/ServiceRegistrar.cs index 28ba295d..e4bba2b5 100644 --- a/src/MediatR/Registration/ServiceRegistrar.cs +++ b/src/MediatR/Registration/ServiceRegistrar.cs @@ -23,8 +23,8 @@ public static void AddMediatRClasses(IServiceCollection services, MediatRService if (configuration.AutoRegisterRequestProcessors) { - ConnectImplementationsToTypesClosing(typeof(IRequestPreProcessor<>), services, assembliesToScan, false, configuration); - ConnectImplementationsToTypesClosing(typeof(IRequestPostProcessor<,>), services, assembliesToScan, false, configuration); + ConnectImplementationsToTypesClosing(typeof(IRequestPreProcessor<>), services, assembliesToScan, true, configuration); + ConnectImplementationsToTypesClosing(typeof(IRequestPostProcessor<,>), services, assembliesToScan, true, configuration); } var multiOpenInterfaces = new List diff --git a/test/MediatR.Tests/MicrosoftExtensionsDI/PipelineTests.cs b/test/MediatR.Tests/MicrosoftExtensionsDI/PipelineTests.cs index 6d5276a1..c09b5ed9 100644 --- a/test/MediatR.Tests/MicrosoftExtensionsDI/PipelineTests.cs +++ b/test/MediatR.Tests/MicrosoftExtensionsDI/PipelineTests.cs @@ -847,7 +847,7 @@ public void Should_handle_open_behaviors_registration_from_a_single_type() } [Fact] - public void Should_auto_register_processors_when_configured() + public void Should_auto_register_processors_when_configured_including_all_concrete_types() { var cfg = new MediatRServiceConfiguration { @@ -864,8 +864,15 @@ public void Should_auto_register_processors_when_configured() var provider = services.BuildServiceProvider(); - provider.GetServices(typeof(IRequestPreProcessor)).Count().ShouldBeGreaterThan(0); - provider.GetServices(typeof(IRequestPostProcessor)).Count().ShouldBeGreaterThan(0); + var preProcessors = provider.GetServices(typeof(IRequestPreProcessor)).ToList(); + preProcessors.Count.ShouldBeGreaterThan(0); + preProcessors.ShouldContain(p => p != null && p.GetType() == typeof(FirstConcretePreProcessor)); + preProcessors.ShouldContain(p => p != null && p.GetType() == typeof(NextConcretePreProcessor)); + + var postProcessors = provider.GetServices(typeof(IRequestPostProcessor)).ToList(); + postProcessors.Count.ShouldBeGreaterThan(0); + postProcessors.ShouldContain(p => p != null && p.GetType() == typeof(FirstConcretePostProcessor)); + postProcessors.ShouldContain(p => p != null && p.GetType() == typeof(NextConcretePostProcessor)); }