コード例 #1
0
        public void Invoke(BehaviorContext context, Action next)
        {
            // for now we cheat and pull it from the behavior context:
            var callbackInvoked = context.Get <bool>(CallbackInvocationBehavior.CallbackInvokedKey);
            var messageHandlers = new LoadedMessageHandlers();

            foreach (var messageToHandle in context.Get <LogicalMessages>())
            {
                var handlerTypedToInvoke = HandlerRegistry.GetHandlerTypes(messageToHandle.MessageType).ToList();

                if (!callbackInvoked && !handlerTypedToInvoke.Any())
                {
                    var error = string.Format("No handlers could be found for message type: {0}", messageToHandle.MessageType);
                    throw new InvalidOperationException(error);
                }

                foreach (var handlerType in handlerTypedToInvoke)
                {
                    messageHandlers.AddHandler(messageToHandle.MessageType, context.Builder.Build(handlerType));
                }
            }

            context.Set(messageHandlers);

            next();
        }
コード例 #2
0
        public void Invoke(BehaviorContext context, Action next)
        {
            // for now we cheat and pull it from the behavior context:
            var callbackInvoked = context.Get<bool>(CallbackInvocationBehavior.CallbackInvokedKey);
            var messageHandlers = new LoadedMessageHandlers();

            foreach (var messageToHandle in context.Get<LogicalMessages>())
            {
                var handlerTypedToInvoke = HandlerRegistry.GetHandlerTypes(messageToHandle.MessageType).ToList();

                if (!callbackInvoked && !handlerTypedToInvoke.Any())
                {
                    var error = string.Format("No handlers could be found for message type: {0}", messageToHandle.MessageType);
                    throw new InvalidOperationException(error);
                }

                foreach (var handlerType in handlerTypedToInvoke)
                {
                    messageHandlers.AddHandler(messageToHandle.MessageType, context.Builder.Build(handlerType));
                }
            }

            context.Set(messageHandlers);

            next();
        }
コード例 #3
0
        void DispatchMessageToHandlersBasedOnType(IBuilder builder, LogicalMessage toHandle, LoadedMessageHandlers loadedHandlers, BehaviorContext context)
        {
            foreach (var loadedHandler in loadedHandlers.GetHandlersFor(toHandle.MessageType))
            {
                if (loadedHandler.InvocationDisabled)
                    continue;

                var handlerInstance = loadedHandler.Instance;
                try
                {
                    //until we have a outgoing pipeline that inherits context from the main one
                    if (handlerInstance is ISaga)
                    {
                        SagaContext.Current = (ISaga) handlerInstance;
                    }

                    var handlerTypeToInvoke = handlerInstance.GetType();

                    //for backwards compatibility (users can have registered their own factory
                    var factory = GetDispatcherFactoryFor(handlerTypeToInvoke, builder);

                    if (factory != null)
                    {
                        var dispatchers = factory.GetDispatcher(handlerTypeToInvoke, builder, toHandle.Instance).ToList();

                        dispatchers.ForEach(dispatch =>
                        {
                            log.DebugFormat("Dispatching message '{0}' to handler '{1}'", toHandle.MessageType, handlerTypeToInvoke);
                            try
                            {
                                dispatch();
                            }
                            catch (Exception e)
                            {
                                log.Warn(handlerTypeToInvoke.Name + " failed handling message.", e);

                                throw new TransportMessageHandlingFailedException(e);
                            }
                        });
                    }
                    else
                    {
                        loadedHandler.Invocation(handlerInstance, toHandle.Instance);
                    }

                    //for now we have to check of the chain is aborted but this will go away when we refactor the handlers to be a subpipeline
                    if (context.ChainAborted)
                    {
                        log.DebugFormat("Handler {0} requested downstream handlers of message {1} to not be invoked", handlerTypeToInvoke,toHandle.MessageType);
                        return;
                    }
                }
                finally
                {
                    SagaContext.Current = null;
                }
            }
        }
コード例 #4
0
        void DispatchMessageToHandlersBasedOnType(IBuilder builder, LogicalMessage toHandle, LoadedMessageHandlers loadedHandlers, BehaviorContext context)
        {
            foreach (var loadedHandler in loadedHandlers.GetHandlersFor(toHandle.MessageType))
            {
                if (loadedHandler.InvocationDisabled)
                {
                    continue;
                }

                var handlerInstance = loadedHandler.Instance;
                try
                {
                    //until we have a outgoing pipeline that inherits context from the main one
                    if (handlerInstance is ISaga)
                    {
                        SagaContext.Current = (ISaga)handlerInstance;
                    }

                    var handlerTypeToInvoke = handlerInstance.GetType();

                    //for backwards compatibility (users can have registered their own factory
                    var factory = GetDispatcherFactoryFor(handlerTypeToInvoke, builder);

                    if (factory != null)
                    {
                        var dispatchers = factory.GetDispatcher(handlerTypeToInvoke, builder, toHandle.Instance).ToList();

                        dispatchers.ForEach(dispatch =>
                        {
                            log.DebugFormat("Dispatching message '{0}' to handler '{1}'", toHandle.MessageType, handlerTypeToInvoke);
                            try
                            {
                                dispatch();
                            }
                            catch (Exception e)
                            {
                                log.Warn(handlerTypeToInvoke.Name + " failed handling message.", e);

                                throw new TransportMessageHandlingFailedException(e);
                            }
                        });
                    }
                    else
                    {
                        loadedHandler.Invocation(handlerInstance, toHandle.Instance);
                    }

                    //for now we have to check of the chain is aborted but this will go away when we refactor the handlers to be a subpipeline
                    if (context.ChainAborted)
                    {
                        log.DebugFormat("Handler {0} requested downstream handlers of message {1} to not be invoked", handlerTypeToInvoke, toHandle.MessageType);
                        return;
                    }
                }
                finally
                {
                    SagaContext.Current = null;
                }
            }
        }