@@ -361,6 +361,11 @@ public class NotAnOpenBehavior : IPipelineBehavior<Ping, Pong>
361361 public Task < Pong > Handle ( Ping request , RequestHandlerDelegate < Pong > next , CancellationToken cancellationToken ) => next ( ) ;
362362 }
363363
364+ public class ThrowingBehavior : IPipelineBehavior < Ping , Pong >
365+ {
366+ public Task < Pong > Handle ( Ping request , RequestHandlerDelegate < Pong > next , CancellationToken cancellationToken ) => throw new Exception ( request . Message ) ;
367+ }
368+
364369 public class NotAnOpenStreamBehavior : IStreamPipelineBehavior < Ping , Pong >
365370 {
366371 public IAsyncEnumerable < Pong > Handle ( Ping request , StreamHandlerDelegate < Pong > next , CancellationToken cancellationToken ) => next ( ) ;
@@ -524,6 +529,27 @@ public void Should_pick_up_base_exception_behaviors()
524529 output . Messages . ShouldContain ( "Logging generic exception" ) ;
525530 }
526531
532+ [ Fact ]
533+ public void Should_handle_exceptions_from_behaviors ( )
534+ {
535+ var output = new Logger ( ) ;
536+ IServiceCollection services = new ServiceCollection ( ) ;
537+ services . AddSingleton ( output ) ;
538+ services . AddMediatR ( cfg =>
539+ {
540+ cfg . RegisterServicesFromAssembly ( typeof ( Ping ) . Assembly ) ;
541+ cfg . AddBehavior < ThrowingBehavior > ( ) ;
542+ } ) ;
543+ var provider = services . BuildServiceProvider ( ) ;
544+
545+ var mediator = provider . GetRequiredService < IMediator > ( ) ;
546+
547+ Should . Throw < Exception > ( async ( ) => await mediator . Send ( new Ping { Message = "Ping" } ) ) ;
548+
549+ output . Messages . ShouldContain ( "Ping Logged by Generic Type" ) ;
550+ output . Messages . ShouldContain ( "Logging generic exception" ) ;
551+ }
552+
527553 [ Fact ]
528554 public void Should_pick_up_exception_actions ( )
529555 {
@@ -648,6 +674,16 @@ public void Should_handle_open_behavior_registration()
648674 cfg . StreamBehaviorsToRegister [ 0 ] . ImplementationFactory . ShouldBeNull ( ) ;
649675 cfg . StreamBehaviorsToRegister [ 0 ] . ImplementationInstance . ShouldBeNull ( ) ;
650676 cfg . StreamBehaviorsToRegister [ 0 ] . Lifetime . ShouldBe ( ServiceLifetime . Transient ) ;
677+
678+ var services = new ServiceCollection ( ) ;
679+
680+ cfg . RegisterServicesFromAssemblyContaining < Ping > ( ) ;
681+
682+ Should . NotThrow ( ( ) =>
683+ {
684+ services . AddMediatR ( cfg ) ;
685+ services . BuildServiceProvider ( ) ;
686+ } ) ;
651687 }
652688
653689 [ Fact ]
@@ -659,16 +695,26 @@ public void Should_handle_inferred_behavior_registration()
659695
660696 cfg . BehaviorsToRegister . Count . ShouldBe ( 2 ) ;
661697
662- cfg . BehaviorsToRegister [ 0 ] . ServiceType . ShouldBe ( typeof ( IPipelineBehavior < , > ) ) ;
698+ cfg . BehaviorsToRegister [ 0 ] . ServiceType . ShouldBe ( typeof ( IPipelineBehavior < Ping , Pong > ) ) ;
663699 cfg . BehaviorsToRegister [ 0 ] . ImplementationType . ShouldBe ( typeof ( InnerBehavior ) ) ;
664700 cfg . BehaviorsToRegister [ 0 ] . ImplementationFactory . ShouldBeNull ( ) ;
665701 cfg . BehaviorsToRegister [ 0 ] . ImplementationInstance . ShouldBeNull ( ) ;
666702 cfg . BehaviorsToRegister [ 0 ] . Lifetime . ShouldBe ( ServiceLifetime . Transient ) ;
667- cfg . BehaviorsToRegister [ 1 ] . ServiceType . ShouldBe ( typeof ( IPipelineBehavior < , > ) ) ;
703+ cfg . BehaviorsToRegister [ 1 ] . ServiceType . ShouldBe ( typeof ( IPipelineBehavior < Ping , Pong > ) ) ;
668704 cfg . BehaviorsToRegister [ 1 ] . ImplementationType . ShouldBe ( typeof ( OuterBehavior ) ) ;
669705 cfg . BehaviorsToRegister [ 1 ] . ImplementationFactory . ShouldBeNull ( ) ;
670706 cfg . BehaviorsToRegister [ 1 ] . ImplementationInstance . ShouldBeNull ( ) ;
671707 cfg . BehaviorsToRegister [ 1 ] . Lifetime . ShouldBe ( ServiceLifetime . Transient ) ;
708+
709+ var services = new ServiceCollection ( ) ;
710+
711+ cfg . RegisterServicesFromAssemblyContaining < Ping > ( ) ;
712+
713+ Should . NotThrow ( ( ) =>
714+ {
715+ services . AddMediatR ( cfg ) ;
716+ services . BuildServiceProvider ( ) ;
717+ } ) ;
672718 }
673719
674720
@@ -681,16 +727,26 @@ public void Should_handle_inferred_stream_behavior_registration()
681727
682728 cfg . StreamBehaviorsToRegister . Count . ShouldBe ( 2 ) ;
683729
684- cfg . StreamBehaviorsToRegister [ 0 ] . ServiceType . ShouldBe ( typeof ( IStreamPipelineBehavior < , > ) ) ;
730+ cfg . StreamBehaviorsToRegister [ 0 ] . ServiceType . ShouldBe ( typeof ( IStreamPipelineBehavior < Ping , Pong > ) ) ;
685731 cfg . StreamBehaviorsToRegister [ 0 ] . ImplementationType . ShouldBe ( typeof ( InnerStreamBehavior ) ) ;
686732 cfg . StreamBehaviorsToRegister [ 0 ] . ImplementationFactory . ShouldBeNull ( ) ;
687733 cfg . StreamBehaviorsToRegister [ 0 ] . ImplementationInstance . ShouldBeNull ( ) ;
688734 cfg . StreamBehaviorsToRegister [ 0 ] . Lifetime . ShouldBe ( ServiceLifetime . Transient ) ;
689- cfg . StreamBehaviorsToRegister [ 1 ] . ServiceType . ShouldBe ( typeof ( IStreamPipelineBehavior < , > ) ) ;
735+ cfg . StreamBehaviorsToRegister [ 1 ] . ServiceType . ShouldBe ( typeof ( IStreamPipelineBehavior < Ping , Pong > ) ) ;
690736 cfg . StreamBehaviorsToRegister [ 1 ] . ImplementationType . ShouldBe ( typeof ( OuterStreamBehavior ) ) ;
691737 cfg . StreamBehaviorsToRegister [ 1 ] . ImplementationFactory . ShouldBeNull ( ) ;
692738 cfg . StreamBehaviorsToRegister [ 1 ] . ImplementationInstance . ShouldBeNull ( ) ;
693739 cfg . StreamBehaviorsToRegister [ 1 ] . Lifetime . ShouldBe ( ServiceLifetime . Transient ) ;
740+
741+ var services = new ServiceCollection ( ) ;
742+
743+ cfg . RegisterServicesFromAssemblyContaining < Ping > ( ) ;
744+
745+ Should . NotThrow ( ( ) =>
746+ {
747+ services . AddMediatR ( cfg ) ;
748+ services . BuildServiceProvider ( ) ;
749+ } ) ;
694750 }
695751
696752 [ Fact ]
@@ -702,16 +758,26 @@ public void Should_handle_inferred_pre_processor_registration()
702758
703759 cfg . RequestPreProcessorsToRegister . Count . ShouldBe ( 2 ) ;
704760
705- cfg . RequestPreProcessorsToRegister [ 0 ] . ServiceType . ShouldBe ( typeof ( IRequestPreProcessor < > ) ) ;
761+ cfg . RequestPreProcessorsToRegister [ 0 ] . ServiceType . ShouldBe ( typeof ( IRequestPreProcessor < Ping > ) ) ;
706762 cfg . RequestPreProcessorsToRegister [ 0 ] . ImplementationType . ShouldBe ( typeof ( FirstConcretePreProcessor ) ) ;
707763 cfg . RequestPreProcessorsToRegister [ 0 ] . ImplementationFactory . ShouldBeNull ( ) ;
708764 cfg . RequestPreProcessorsToRegister [ 0 ] . ImplementationInstance . ShouldBeNull ( ) ;
709765 cfg . RequestPreProcessorsToRegister [ 0 ] . Lifetime . ShouldBe ( ServiceLifetime . Transient ) ;
710- cfg . RequestPreProcessorsToRegister [ 1 ] . ServiceType . ShouldBe ( typeof ( IRequestPreProcessor < > ) ) ;
766+ cfg . RequestPreProcessorsToRegister [ 1 ] . ServiceType . ShouldBe ( typeof ( IRequestPreProcessor < Ping > ) ) ;
711767 cfg . RequestPreProcessorsToRegister [ 1 ] . ImplementationType . ShouldBe ( typeof ( NextConcretePreProcessor ) ) ;
712768 cfg . RequestPreProcessorsToRegister [ 1 ] . ImplementationFactory . ShouldBeNull ( ) ;
713769 cfg . RequestPreProcessorsToRegister [ 1 ] . ImplementationInstance . ShouldBeNull ( ) ;
714770 cfg . RequestPreProcessorsToRegister [ 1 ] . Lifetime . ShouldBe ( ServiceLifetime . Transient ) ;
771+
772+ var services = new ServiceCollection ( ) ;
773+
774+ cfg . RegisterServicesFromAssemblyContaining < Ping > ( ) ;
775+
776+ Should . NotThrow ( ( ) =>
777+ {
778+ services . AddMediatR ( cfg ) ;
779+ services . BuildServiceProvider ( ) ;
780+ } ) ;
715781 }
716782
717783 [ Fact ]
@@ -723,16 +789,26 @@ public void Should_handle_inferred_post_processor_registration()
723789
724790 cfg . RequestPostProcessorsToRegister . Count . ShouldBe ( 2 ) ;
725791
726- cfg . RequestPostProcessorsToRegister [ 0 ] . ServiceType . ShouldBe ( typeof ( IRequestPostProcessor < , > ) ) ;
792+ cfg . RequestPostProcessorsToRegister [ 0 ] . ServiceType . ShouldBe ( typeof ( IRequestPostProcessor < Ping , Pong > ) ) ;
727793 cfg . RequestPostProcessorsToRegister [ 0 ] . ImplementationType . ShouldBe ( typeof ( FirstConcretePostProcessor ) ) ;
728794 cfg . RequestPostProcessorsToRegister [ 0 ] . ImplementationFactory . ShouldBeNull ( ) ;
729795 cfg . RequestPostProcessorsToRegister [ 0 ] . ImplementationInstance . ShouldBeNull ( ) ;
730796 cfg . RequestPostProcessorsToRegister [ 0 ] . Lifetime . ShouldBe ( ServiceLifetime . Transient ) ;
731- cfg . RequestPostProcessorsToRegister [ 1 ] . ServiceType . ShouldBe ( typeof ( IRequestPostProcessor < , > ) ) ;
797+ cfg . RequestPostProcessorsToRegister [ 1 ] . ServiceType . ShouldBe ( typeof ( IRequestPostProcessor < Ping , Pong > ) ) ;
732798 cfg . RequestPostProcessorsToRegister [ 1 ] . ImplementationType . ShouldBe ( typeof ( NextConcretePostProcessor ) ) ;
733799 cfg . RequestPostProcessorsToRegister [ 1 ] . ImplementationFactory . ShouldBeNull ( ) ;
734800 cfg . RequestPostProcessorsToRegister [ 1 ] . ImplementationInstance . ShouldBeNull ( ) ;
735801 cfg . RequestPostProcessorsToRegister [ 1 ] . Lifetime . ShouldBe ( ServiceLifetime . Transient ) ;
802+
803+ var services = new ServiceCollection ( ) ;
804+
805+ cfg . RegisterServicesFromAssemblyContaining < Ping > ( ) ;
806+
807+ Should . NotThrow ( ( ) =>
808+ {
809+ services . AddMediatR ( cfg ) ;
810+ services . BuildServiceProvider ( ) ;
811+ } ) ;
736812 }
737813
738814 [ Fact ]
@@ -756,5 +832,15 @@ public void Should_handle_open_behaviors_registration_from_a_single_type()
756832 cfg . StreamBehaviorsToRegister [ 0 ] . ImplementationFactory . ShouldBeNull ( ) ;
757833 cfg . StreamBehaviorsToRegister [ 0 ] . ImplementationInstance . ShouldBeNull ( ) ;
758834 cfg . StreamBehaviorsToRegister [ 0 ] . Lifetime . ShouldBe ( ServiceLifetime . Singleton ) ;
835+
836+ var services = new ServiceCollection ( ) ;
837+
838+ cfg . RegisterServicesFromAssemblyContaining < Ping > ( ) ;
839+
840+ Should . NotThrow ( ( ) =>
841+ {
842+ services . AddMediatR ( cfg ) ;
843+ services . BuildServiceProvider ( ) ;
844+ } ) ;
759845 }
760846}
0 commit comments