@@ -142,29 +142,27 @@ public async Task Subscribe<TMessage>(params string[] topics)
142142
143143 if ( msg . Value == null ) continue ;
144144
145- using ( var scope = _serviceProvider . CreateScope ( ) )
146- {
147- var mediator = scope . ServiceProvider . GetRequiredService < IMediator > ( ) ;
148- var noti = msg . Value . MapTo < TMessage , INotification > ( ) ;
149-
150- var msgField = msg . Value . Descriptor . FindFieldByName ( "Key" ) ;
151- var msgValue = msgField . Accessor . GetValue ( msg . Value ) ;
152-
153- var currentAssembly = Assembly . GetExecutingAssembly ( ) ;
154- var callerAssemblies = new StackTrace ( ) . GetFrames ( )
155- . Select ( x => x . GetMethod ( ) . ReflectedType . Assembly ) . Distinct ( )
156- . Where ( x => x . GetReferencedAssemblies ( ) . Any ( y => y . FullName == currentAssembly . FullName ) ) ;
145+ var keyField = msg . Value . Descriptor . FindFieldByName ( "Key" ) ;
146+ var key = keyField . Accessor . GetValue ( msg . Value ) ;
157147
158- var initialAssembly = callerAssemblies . Last ( ) ;
148+ var currentAssembly = Assembly . GetExecutingAssembly ( ) ;
149+ var callerAssemblies = new StackTrace ( )
150+ . GetFrames ( )
151+ ? . Select ( x => x . GetMethod ( ) . ReflectedType ? . Assembly ) . Distinct ( )
152+ . Where ( x => x . GetReferencedAssemblies ( ) . Any ( y => y . FullName == currentAssembly . FullName ) ) ;
153+ var callerAssembly = callerAssemblies ? . Last ( ) ;
159154
160- var notific = initialAssembly . DefinedTypes
161- . FirstOrDefault ( x => x . AssemblyQualifiedName . Contains ( msgValue . ToString ( ) ) ) ;
155+ var notifyType = callerAssembly
156+ ? . DefinedTypes
157+ . FirstOrDefault ( x => x . AssemblyQualifiedName . Contains ( key . ToString ( ) ) ) ;
162158
163- var obj = ( INotification ) Activator . CreateInstance ( notific ) ;
159+ var notify = ( INotification ) Activator . CreateInstance ( notifyType ) ;
160+ var notifyInstance = Mapper . Map ( msg . Value , notify ) ;
164161
165- var abc = Mapper . Map ( msg . Value , obj ) ;
166-
167- await mediator . Publish ( abc ) ;
162+ using ( var scope = _serviceProvider . CreateScope ( ) )
163+ {
164+ var mediator = scope . ServiceProvider . GetRequiredService < IMediator > ( ) ;
165+ await mediator . Publish ( notifyInstance ) ;
168166 }
169167 }
170168 }
0 commit comments