Exemple #1
0
        public Task <CommandResult> Send(ICommand message)
        {
            if (!IsRunning)
            {
                throw new InvalidOperationException("Unable to send message, the bus is not running");
            }

            var peers = _directory.GetPeersHandlingMessage(message);

            if (peers.Count == 0)
            {
                throw new InvalidOperationException("Unable to find peer for specified command, " + BusMessageLogger.ToString(message) + ". Did you change the namespace?");
            }

            var self = peers.FirstOrDefault(x => x.Id == PeerId);

            if (self != null)
            {
                return(Send(message, self));
            }

            if (peers.Count > 1)
            {
                var exceptionMessage = $"{peers.Count} peers are handling {BusMessageLogger.ToString(message)}. Peers: {string.Join(", ", peers.Select(p => p.ToString()))}.";
                throw new InvalidOperationException(exceptionMessage);
            }

            return(Send(message, peers[0]));
        }
Exemple #2
0
        public Task <CommandResult> Send(ICommand message, Peer peer)
        {
            if (peer == null)
            {
                throw new ArgumentNullException(nameof(peer));
            }

            if (!IsRunning)
            {
                throw new InvalidOperationException("Unable to send message, the bus is not running");
            }

            var taskCompletionSource = new TaskCompletionSource <CommandResult>();

            if (LocalDispatch.Enabled && peer.Id == PeerId)
            {
                HandleLocalMessage(message, taskCompletionSource);
            }
            else
            {
                var transportMessage = ToTransportMessage(message);

                if (!peer.IsResponding && !_messageSendingStrategy.IsMessagePersistent(transportMessage) && !message.TypeId().IsInfrastructure())
                {
                    throw new InvalidOperationException($"Unable to send this transient message {BusMessageLogger.ToString(message)} while peer {peer.Id} is not responding.");
                }

                _messageIdToTaskCompletionSources.TryAdd(transportMessage.Id, taskCompletionSource);

                var peers = new[] { peer };
                LogMessageSend(message, transportMessage, peers);
                SendTransportMessage(transportMessage, peers);
            }

            return(taskCompletionSource.Task);
        }
Exemple #3
0
        public Task <CommandResult> Send(ICommand message, Peer peer)
        {
            if (peer == null)
            {
                throw new ArgumentNullException("peer");
            }

            if (!_isRunning)
            {
                throw new InvalidOperationException("Unable to send message, the bus is not running");
            }

            var taskCompletionSource = new TaskCompletionSource <CommandResult>();

            if (LocalDispatch.Enabled && peer.Id == _peerId)
            {
                HandleLocalMessage(message, taskCompletionSource);
            }
            else
            {
                if (!peer.IsResponding && !message.TypeId().IsPersistent() && !message.TypeId().IsInfrastructure())
                {
                    var exceptionMessage = string.Format("Unable to send this transient message {0} while peer {1} is not responding.", BusMessageLogger.ToString(message), peer.Id);
                    throw new InvalidOperationException(exceptionMessage);
                }
                var messageId = MessageId.NextId();
                _messageIdToTaskCompletionSources.TryAdd(messageId, taskCompletionSource);
                SendTransportMessage(messageId, message, peer, true);
            }

            return(taskCompletionSource.Task);
        }