Example #1
0
        private void RunBatch(Batch batch)
        {
            if (batch.IsEmpty)
            {
                return;
            }

            try
            {
                if (!_isRunning)
                {
                    return;
                }

                switch (batch.FirstEntry.Invoker.Mode)
                {
                case MessageHandlerInvokerMode.Synchronous:
                {
                    SynchronizationContext.SetSynchronizationContext(null);
                    var invocation = _pipeManager.BuildPipeInvocation(batch.FirstEntry.Invoker, batch.Messages, batch.FirstEntry.Dispatch.Context);
                    invocation.Run();
                    batch.SetHandled(null);
                    break;
                }

                case MessageHandlerInvokerMode.Asynchronous:
                {
                    SynchronizationContext.SetSynchronizationContext(SynchronizationContext);
                    var asyncBatch = batch.Clone();
                    var invocation = _pipeManager.BuildPipeInvocation(asyncBatch.FirstEntry.Invoker, asyncBatch.Messages, asyncBatch.FirstEntry.Dispatch.Context);
                    Interlocked.Increment(ref _asyncInvocationsCount);
                    invocation.RunAsync().ContinueWith(task => OnAsyncBatchCompleted(task, asyncBatch), TaskContinuationOptions.ExecuteSynchronously);
                    break;
                }

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                batch.SetHandled(ex);
            }
            finally
            {
                batch.Clear();
                LocalDispatch.Reset();
            }
        }
Example #2
0
 private void RunAction(Action action)
 {
     try
     {
         SynchronizationContext.SetSynchronizationContext(SynchronizationContext);
         action();
     }
     catch (Exception ex)
     {
         _logger.Error(ex);
     }
     finally
     {
         LocalDispatch.Reset();
     }
 }