Inheritance: DispatcherInvocationContext
示例#1
0
        public void Dispatch(Object message)
        {
            try
            {
                var handlerTypes = _registry.GetHandlersType(message.GetType());

                foreach (var handlerType in handlerTypes)
                {
                    var handler = _serviceLocator.GetInstance(handlerType);

                    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)
                        {
                            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);
                                
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                throw new DispatchingException("Error when dispatching message", exception);
            }
        }
        public void Dispatch(Object message)
        {
            try
            {
                var handlerTypes = _registry.GetHandlersType(message.GetType());

                foreach (var handlerType in handlerTypes)
                {
                    var handler = _serviceLocator.GetInstance(handlerType);

                    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)
                        {
                            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);
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                throw new DispatchingException("Error when dispatching message", exception);
            }
        }