public void Intercept(DispatcherInvocationContext context)
        {
            if (context.Message == null)
                return;

            var command = context.Message as ICommand;

            if (command != null)
            {
                try
                {
                    LogManager.LogCommand(command);
                    context.Invoke();
                    LogManager.LogCommandHandler(command.Metadata.CommandId, context.Handler.GetType().FullName);
                }
                catch(Exception ex)
                {
                    LogManager.LogCommandHandler(command.Metadata.CommandId, context.Handler.GetType().FullName, ex);
                    throw;
                }

                return;
            }

            var evnt = context.Message as IEvent;

            if (evnt != null)
            {
                try
                {
                    LogManager.LogEvent(evnt);
                    context.Invoke();
                    LogManager.LogEventHandler(evnt.Metadata.CommandId, evnt.Metadata.EventId, context.Handler.GetType().FullName);
                }
                catch (Exception ex)
                {
                    LogManager.LogEventHandler(evnt.Metadata.CommandId, evnt.Metadata.EventId, context.Handler.GetType().FullName, ex);
                    throw;
                }

                return;
            }

            // if this is not command or event - execute as usual
            context.Invoke();
        }
Exemple #2
0
        private void ExecuteHandler(Object handler, Object message, Action<Exception> exceptionObserver = null)
        {
            var attempt = 0;
            while (attempt < _maxRetries)
            {
                try
                {
                    var context = new DispatcherInvocationContext(this, handler, message);

                    if (_registry.Interceptors.Count > 0)
                    {
                        // Call interceptors in backward order
                        for (int i = _registry.Interceptors.Count - 1; i >= 0; i--)
                        {
                            var interceptorType = _registry.Interceptors[i];
                            var interceptor = (IMessageHandlerInterceptor)_serviceLocator.GetInstance(interceptorType);
                            context = new DispatcherInterceptorContext(interceptor, context);
                        }
                    }

                    context.Invoke();

                    // message handled correctly - so that should be
                    // the final attempt
                    attempt = _maxRetries;
                }
                catch (Exception exception)
                {
                    if (exceptionObserver != null)
                        exceptionObserver(exception);

                    attempt++;

                    if (attempt == _maxRetries)
                    {
                        throw new HandlerException(String.Format(
                            "Exception in the handler {0} for message {1}", handler.GetType().FullName, message.GetType().FullName), exception, message);

                    }
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="T:System.Object"/> class.
 /// </summary>
 public DispatcherInterceptorContext(IMessageHandlerInterceptor interceptor, DispatcherInvocationContext context)
 {
     _interceptor = interceptor;
     _context = context;
 }