Exemplo n.º 1
0
        public Task Send(ICommand command, CancellationToken cancellationToken)
        {
            var currentMessageContext = PerMessageContextLifetimeManager.CurrentMessageContext;

            if (currentMessageContext != null && currentMessageContext.Message is ICommand)
            {
                // A command sent in a CommandContext is not allowed. We throw exception!!!
                throw new NotSupportedException("Command is not allowd to be sent in another command context!");
            }

            string commandKey = null;

            if (command is ILinearCommand)
            {
                var linearKey = LinearCommandManager.GetLinearKey(command as ILinearCommand);
                if (linearKey != null)
                {
                    commandKey = linearKey.ToString();
                }
            }
            IMessageContext commandContext = null;

            commandContext = new MessageContext(CommandTopic, command, ReplyTopic, commandKey);
            Task task = null;

            if (InProc && currentMessageContext == null && !(command is ILinearCommand))
            {
                task = SendInProc(commandContext, cancellationToken);
            }
            else
            {
                if (currentMessageContext != null)
                {
                    ((MessageContext)currentMessageContext).ToBeSentMessageContexts.Add(commandContext);
                }
                else
                {
                    task = SendAsync(commandContext, cancellationToken);
                }
            }
            return(task);
        }
Exemplo n.º 2
0
        public void Add(ICommand command)
        {
            var currentMessageContext = PerMessageContextLifetimeManager.CurrentMessageContext;

            if (currentMessageContext == null)
            {
                throw new CurrentMessageContextIsNull();
            }
            string commandKey = null;

            if (command is ILinearCommand)
            {
                var linearKey = LinearCommandManager.GetLinearKey(command as ILinearCommand);
                if (linearKey != null)
                {
                    commandKey = linearKey.ToString();
                }
            }
            IMessageContext commandContext = new MessageContext(ReplyTopic, command, commandKey);

            ((MessageContext)currentMessageContext).ToBeSentMessageContexts.Add(commandContext);
        }