@@ -17,6 +17,21 @@ IAsyncEnumerable<TResponse> CreateStream<TRequest, TResponse>(IStreamRequest<TRe
1717
1818 ValueTask Publish < TNotification > ( TNotification request , CancellationToken cancellationToken )
1919 where TNotification : INotification ;
20+
21+ /// <summary>
22+ /// This method is not recommended for performance-critical scenarios.
23+ /// Use it only if it is strictly necessary, as its performance is lower compared
24+ /// to similar methods in terms of both memory usage and CPU consumption.
25+ /// </summary>
26+ /// <param name="request">
27+ /// An object that implements INotification
28+ /// </param>
29+ /// <param name="cancellationToken"></param>
30+ /// <returns></returns>
31+ [ Obsolete ( message : "This method has performance issues. Use only if strictly necessary" ,
32+ error : false ,
33+ DiagnosticId = Constants . DiagnosticPerformanceIssue ) ]
34+ ValueTask Publish ( object request , CancellationToken cancellationToken ) ;
2035}
2136
2237public sealed class Mediator ( IServiceProvider serviceProvider ) : IMediator
@@ -35,17 +50,18 @@ public TResponse Send<TRequest, TResponse>(IRequest<TRequest, TResponse> request
3550 }
3651 }
3752
38- public IAsyncEnumerable < TResponse > CreateStream < TRequest , TResponse > ( IStreamRequest < TRequest , TResponse > request ,
53+ public IAsyncEnumerable < TResponse > CreateStream < TRequest , TResponse > ( IStreamRequest < TRequest , TResponse > request ,
3954 CancellationToken cancellationToken ) where TRequest : class , IStreamRequest
4055 {
4156 return serviceProvider . GetRequiredService < IStreamRequestHandler < TRequest , TResponse > > ( )
4257 . Handle ( Unsafe . As < TRequest > ( request ) , cancellationToken ) ;
4358 }
4459
45- public async ValueTask Publish < TNotification > ( TNotification request , CancellationToken cancellationToken ) where TNotification : INotification
60+ public async ValueTask Publish < TNotification > ( TNotification request , CancellationToken cancellationToken )
61+ where TNotification : INotification
4662 {
4763 var notificationsInDi = serviceProvider . GetRequiredService < IEnumerable < INotificationHandler < TNotification > > > ( ) ;
48-
64+
4965 var notifications = Unsafe . As < INotificationHandler < TNotification > [ ] > ( notificationsInDi ) ;
5066 foreach ( var notification in notifications )
5167 {
@@ -56,4 +72,26 @@ public async ValueTask Publish<TNotification>(TNotification request, Cancellatio
5672 }
5773 }
5874 }
75+
76+ public async ValueTask Publish ( object request , CancellationToken cancellationToken )
77+ {
78+ ArgumentNullException . ThrowIfNull ( request ) ;
79+
80+ var requestType = request . GetType ( ) ;
81+ var handlerType = typeof ( INotificationHandler < > ) . MakeGenericType ( requestType ) ;
82+
83+ var notificationsInDi = serviceProvider . GetServices ( handlerType ) ;
84+
85+ foreach ( var handler in notificationsInDi )
86+ {
87+ var handleMethod = handlerType . GetMethod ( nameof ( INotificationHandler < INotification > . Handle ) ) ;
88+ ArgumentNullException . ThrowIfNull ( handleMethod ) ;
89+
90+ var valueTask = ( ValueTask ? ) handleMethod . Invoke ( handler , [ request , cancellationToken ] ) ;
91+ ArgumentNullException . ThrowIfNull ( valueTask ) ;
92+
93+ if ( ! valueTask . Value . IsCompletedSuccessfully )
94+ await valueTask . Value ;
95+ }
96+ }
5997}
0 commit comments