@@ -149,6 +149,8 @@ private void RemoveRpcException()
149149 /// </summary>
150150 public SMD MetaData { get ; set ; }
151151
152+ private const string THREAD_CALLBACK_SLOT_NAME = "Callback" ;
153+
152154 #region Public Methods
153155
154156 /// <summary>
@@ -199,7 +201,9 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null, Action<Jso
199201 Error = preProcessingException ,
200202 Id = Rpc . Id
201203 } ;
204+ //callback is called - if it is empty then nothing will be done
202205 callback . Invoke ( response ) ;
206+ //return response always- if callback is empty or not
203207 return response ;
204208 }
205209
@@ -339,11 +343,12 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null, Action<Jso
339343 try
340344 {
341345 //callback is stored to thread's local storage in order to get it directly from concrete JsonRpcService method implementation
342- if ( null != callback )
343- {
344- Thread . SetData ( Thread . GetNamedDataSlot ( "Callback" ) , callback ) ;
345- }
346+ //where callback is just returned from method
347+ Thread . SetData ( Thread . GetNamedDataSlot ( THREAD_CALLBACK_SLOT_NAME ) , callback ) ;
348+
349+
346350 var results = handle . DynamicInvoke ( parameters ) ;
351+
347352 var last = parameters . LastOrDefault ( ) ;
348353 JsonRpcException contextException ;
349354 if ( Task . CurrentId . HasValue && RpcExceptions . TryRemove ( Task . CurrentId . Value , out contextException ) )
@@ -358,7 +363,8 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null, Action<Jso
358363 callback . Invoke ( response ) ;
359364 return response ;
360365 }
361-
366+ //return response, if callback is set (method is asynchronous) - result could be empty string and future result operations
367+ //will be processed in the callback
362368 return new JsonResponse ( ) { Result = results } ;
363369 }
364370 catch ( Exception ex )
@@ -401,6 +407,26 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null, Action<Jso
401407 }
402408 }
403409
410+ /// <summary>
411+ /// Method returns the actual callback set to this thread in Handle() method.
412+ /// If callback is not set, then empty callback is returned.
413+ /// </summary>
414+ /// <returns></returns>
415+ internal Action < JsonResponse > GetAsyncCallback ( )
416+ {
417+ object o = Thread . GetData ( Thread . GetNamedDataSlot ( THREAD_CALLBACK_SLOT_NAME ) ) ;
418+ Action < JsonResponse > callback ;
419+ if ( o is Action < JsonResponse > )
420+ {
421+ callback = o as Action < JsonResponse > ;
422+ }
423+ else
424+ {
425+ callback = delegate ( JsonResponse a ) { } ;
426+ }
427+ return callback ;
428+ }
429+
404430 private void AddRpcContext ( object RpcContext )
405431 {
406432 if ( Task . CurrentId != null )
0 commit comments