@@ -969,4 +969,96 @@ public async Task Should_register_correctly()
969969 "Invoked Handler" ,
970970 } ) ;
971971 }
972+
973+
974+ #region OpenBehaviorsForMultipleRegistration
975+ sealed class OpenBehaviorMultipleRegistration0 < TRequest , TResponse > : IPipelineBehavior < TRequest , TResponse >
976+ where TRequest : notnull
977+ {
978+ public OpenBehaviorMultipleRegistration0 ( IBlogger < OpenBehaviorMultipleRegistration0 < TRequest , TResponse > > logger )
979+ {
980+ this . logger = logger ;
981+ }
982+
983+ readonly IBlogger < OpenBehaviorMultipleRegistration0 < TRequest , TResponse > > logger ;
984+
985+ public Task < TResponse > Handle ( TRequest request , RequestHandlerDelegate < TResponse > next , CancellationToken cancellationToken )
986+ {
987+ logger . Messages . Add ( "Invoked OpenBehaviorMultipleRegistration0" ) ;
988+ return next ( ) ;
989+ }
990+ }
991+ sealed class OpenBehaviorMultipleRegistration1 < TRequest , TResponse > : IPipelineBehavior < TRequest , TResponse >
992+ where TRequest : notnull
993+ {
994+ public OpenBehaviorMultipleRegistration1 ( IBlogger < OpenBehaviorMultipleRegistration1 < TRequest , TResponse > > logger )
995+ {
996+ this . logger = logger ;
997+ }
998+
999+ readonly IBlogger < OpenBehaviorMultipleRegistration1 < TRequest , TResponse > > logger ;
1000+
1001+ public Task < TResponse > Handle ( TRequest request , RequestHandlerDelegate < TResponse > next , CancellationToken cancellationToken )
1002+ {
1003+ logger . Messages . Add ( "Invoked OpenBehaviorMultipleRegistration1" ) ;
1004+ return next ( ) ;
1005+ }
1006+ }
1007+ sealed class OpenBehaviorMultipleRegistration2 < TRequest , TResponse > : IPipelineBehavior < TRequest , TResponse >
1008+ where TRequest : notnull
1009+ {
1010+ public OpenBehaviorMultipleRegistration2 ( IBlogger < OpenBehaviorMultipleRegistration2 < TRequest , TResponse > > logger )
1011+ {
1012+ this . logger = logger ;
1013+ }
1014+
1015+ readonly IBlogger < OpenBehaviorMultipleRegistration2 < TRequest , TResponse > > logger ;
1016+
1017+ public Task < TResponse > Handle ( TRequest request , RequestHandlerDelegate < TResponse > next , CancellationToken cancellationToken )
1018+ {
1019+ logger . Messages . Add ( "Invoked OpenBehaviorMultipleRegistration2" ) ;
1020+ return next ( ) ;
1021+ }
1022+ }
1023+ #endregion OpenBehaviorsForMultipleRegistration
1024+
1025+ [ Fact ]
1026+ public async Task Should_register_open_behaviors_correctly ( )
1027+ {
1028+ var behaviorTypeList = new List < Type >
1029+ {
1030+ typeof ( OpenBehaviorMultipleRegistration0 < , > ) ,
1031+ typeof ( OpenBehaviorMultipleRegistration1 < , > ) ,
1032+ typeof ( OpenBehaviorMultipleRegistration2 < , > )
1033+ } ;
1034+ var services = new ServiceCollection ( ) ;
1035+ services . AddMediatR ( cfg =>
1036+ {
1037+ cfg . RegisterServicesFromAssemblyContaining < FooRequest > ( ) ;
1038+ cfg . AddOpenBehaviors ( behaviorTypeList ) ;
1039+ } ) ;
1040+ var logger = new Logger ( ) ;
1041+ services . AddSingleton ( logger ) ;
1042+ services . AddSingleton ( new MediatR . Tests . PipelineTests . Logger ( ) ) ;
1043+ services . AddSingleton ( new MediatR . Tests . StreamPipelineTests . Logger ( ) ) ;
1044+ services . AddSingleton ( new MediatR . Tests . SendTests . Dependency ( ) ) ;
1045+ services . AddSingleton < System . IO . TextWriter > ( new System . IO . StringWriter ( ) ) ;
1046+ services . AddTransient ( typeof ( IBlogger < > ) , typeof ( Blogger < > ) ) ;
1047+ var provider = services . BuildServiceProvider ( new ServiceProviderOptions
1048+ {
1049+ ValidateOnBuild = true
1050+ } ) ;
1051+
1052+ var mediator = provider . GetRequiredService < IMediator > ( ) ;
1053+ var request = new FooRequest ( ) ;
1054+ await mediator . Send ( request ) ;
1055+
1056+ logger . Messages . ShouldBe ( new [ ]
1057+ {
1058+ "Invoked OpenBehaviorMultipleRegistration0" ,
1059+ "Invoked OpenBehaviorMultipleRegistration1" ,
1060+ "Invoked OpenBehaviorMultipleRegistration2" ,
1061+ "Invoked Handler" ,
1062+ } ) ;
1063+ }
9721064}
0 commit comments