示例#1
0
        public RouteContext GetContext(RouteId id, HttpContext ctx)
        {
            var spec    = Specs[id];
            var options = RouteOptionsBuilder.Build(id, Config);
            var callCtx = RequestContextBuilder.Build(ctx);
            var handler = HandlerProvider.GetHandler(options.Handler, options.Config, responseContext: null);

            return(new RouteContext(options, spec, callCtx, handler, Logger));
        }
示例#2
0
        Task SendInProc(IMessageContext commandContext, CancellationToken cancellationToken)
        {
            Task task    = null;
            var  command = commandContext.Message as ICommand;

            if (command is ILinearCommand)
            {
                task = SendAsync(commandContext, cancellationToken);
            }
            else //if not a linear command, we run synchronously.
            {
                task = new Task(() =>
                {
                    var commandHandler = HandlerProvider.GetHandler(command.GetType());
                    if (commandHandler == null)
                    {
                        throw new NoCommandHandlerExists();
                    }

                    MessageState messageState = BuildMessageState(commandContext.MessageID, command, cancellationToken);
                    PerMessageContextLifetimeManager.CurrentMessageContext = commandContext;
                    try
                    {
                        var unitOfWork = IoCFactory.Resolve <IUnitOfWork>();
                        commandHandler.Handle(command);
                        unitOfWork.Commit();
                    }
                    finally
                    {
                        commandContext.ClearItems();
                    }
                });
                task.RunSynchronously();
                if (task.Exception != null)
                {
                    throw task.Exception.GetBaseException();
                }
            }
            return(task);
        }
 private static Type GetHandler <TAction>(TAction action)
     where TAction : IAction
 {
     return(HandlerProvider.GetHandler <IActionHandler <TAction> >());
 }
示例#4
0
        protected virtual Task SendInProc(IMessageContext commandContext, CancellationToken cancellationToken)
        {
            Task task    = null;
            var  command = commandContext.Message as ICommand;

            if (command is ILinearCommand)
            {
                task = SendAsync(commandContext, cancellationToken);
            }
            else if (command != null) //if not a linear command, we run synchronously.
            {
                task = Task.Factory.StartNew(() =>
                {
                    IMessageStore messageStore = null;
                    try
                    {
                        var needRetry = command.NeedRetry;
                        object result = null;
                        PerMessageContextLifetimeManager.CurrentMessageContext = commandContext;
                        messageStore = IoCFactory.Resolve <IMessageStore>();

                        if (!messageStore.HasCommandHandled(commandContext.MessageID))
                        {
                            var commandHandler = HandlerProvider.GetHandler(command.GetType());
                            if (commandHandler == null)
                            {
                                PerMessageContextLifetimeManager.CurrentMessageContext = null;
                                throw new NoHandlerExists();
                            }

                            do
                            {
                                try
                                {
                                    ((dynamic)commandHandler).Handle((dynamic)command);
                                    result    = commandContext.Reply;
                                    needRetry = false;
                                }
                                catch (Exception ex)
                                {
                                    if (!(ex is OptimisticConcurrencyException) || !needRetry)
                                    {
                                        throw;
                                    }
                                }
                            } while (needRetry);
                            return(result);
                        }
                        else
                        {
                            throw new MessageDuplicatelyHandled();
                        }
                    }
                    catch (Exception e)
                    {
                        if (e is DomainException)
                        {
                            _Logger.Warn(command.ToJson(), e);
                        }
                        else
                        {
                            _Logger.Error(command.ToJson(), e);
                        }
                        if (messageStore != null)
                        {
                            messageStore.SaveFailedCommand(commandContext);
                        }
                        throw;
                    }
                    finally
                    {
                        PerMessageContextLifetimeManager.CurrentMessageContext = null;
                    }
                }, cancellationToken);
            }
            return(task);
        }
示例#5
0
        protected override void ConsumeMessage(IFramework.MessageQueue.EQueue.MessageFormat.MessageContext messageContext, EQueueProtocols.QueueMessage queueMessage)
        {
            if (messageContext == null || messageContext.Message == null)
            {
                return;
            }
            var message = messageContext.Message as ICommand;

            if (message == null)
            {
                return;
            }
            MessageReply  messageReply      = null;
            var           needRetry         = message.NeedRetry;
            bool          commandHasHandled = false;
            IMessageStore messageStore      = null;

            try
            {
                PerMessageContextLifetimeManager.CurrentMessageContext = messageContext;
                messageStore      = IoCFactory.Resolve <IMessageStore>();
                commandHasHandled = messageStore.HasCommandHandled(messageContext.MessageID);
                if (!commandHasHandled)
                {
                    var messageHandler = HandlerProvider.GetHandler(message.GetType());
                    _Logger.InfoFormat("Handle command, commandID:{0}", messageContext.MessageID);

                    if (messageHandler == null)
                    {
                        messageReply = new MessageReply(messageContext.ReplyToEndPoint, messageContext.MessageID, new NoHandlerExists());
                    }
                    else
                    {
                        do
                        {
                            try
                            {
                                ((dynamic)messageHandler).Handle((dynamic)message);
                                messageReply = new MessageReply(messageContext.ReplyToEndPoint, messageContext.MessageID, messageContext.Reply);
                                needRetry    = false;
                            }
                            catch (Exception ex)
                            {
                                if (!(ex is OptimisticConcurrencyException) || !needRetry)
                                {
                                    throw;
                                }
                            }
                        } while (needRetry);
                    }
                }
                else
                {
                    messageReply = new MessageReply(messageContext.ReplyToEndPoint, messageContext.MessageID, new MessageDuplicatelyHandled());
                }
            }
            catch (Exception e)
            {
                messageReply = new MessageReply(messageContext.ReplyToEndPoint, messageContext.MessageID, e.GetBaseException());
                if (e is DomainException)
                {
                    _Logger.Warn(message.ToJson(), e);
                }
                else
                {
                    _Logger.Error(message.ToJson(), e);
                }
                if (messageStore != null)
                {
                    messageStore.SaveFailedCommand(messageContext);
                }
            }
            finally
            {
                PerMessageContextLifetimeManager.CurrentMessageContext = null;
            }
            if (!commandHasHandled)
            {
                OnMessageHandled(messageContext, messageReply);
                HandledMessageCount++;
            }
        }