Exemplo n.º 1
0
        IEnumerable <string> SendMessage(List <Address> addresses, string correlationId, MessageIntentEnum messageIntent, params object[] messages)
        {
            if (messages.Length == 0)
            {
                return(Enumerable.Empty <string>());
            }

            messages.ToList()
            .ForEach(message => MessagingBestPractices.AssertIsValidForSend(message.GetType(), messageIntent));

            if (messages.Length > 1)
            {
                // Users can't send more than one message with a DataBusProperty in the same TransportMessage, Yes this is a limitation for now!
                var numberOfMessagesWithDataBusProperties = 0;
                foreach (var message in messages)
                {
                    var hasAtLeastOneDataBusProperty = message.GetType().GetProperties().Any(MessageConventionExtensions.IsDataBusProperty);

                    if (hasAtLeastOneDataBusProperty)
                    {
                        numberOfMessagesWithDataBusProperties++;
                    }
                }

                if (numberOfMessagesWithDataBusProperties > 1)
                {
                    throw new InvalidOperationException("This version of NServiceBus only supports sending up to one message with DataBusProperties per Send().");
                }
            }

            addresses
            .ForEach(address =>
            {
                if (address == Address.Undefined)
                {
                    throw new InvalidOperationException("No destination specified for message(s): " +
                                                        string.Join(";", messages.Select(m => m.GetType())));
                }
            });



            var result = new List <string>();

            var toSend = new TransportMessage {
                MessageIntent = messageIntent
            };

            if (!string.IsNullOrEmpty(correlationId))
            {
                toSend.CorrelationId = correlationId;
            }

            MapTransportMessageFor(messages, toSend);

            foreach (var destination in addresses)
            {
                try
                {
                    MessageSender.Send(toSend, destination);
                }
                catch (QueueNotFoundException ex)
                {
                    throw new ConfigurationErrorsException("The destination queue '" + destination +
                                                           "' could not be found. You may have misconfigured the destination for this kind of message (" +
                                                           messages[0].GetType().FullName +
                                                           ") in the MessageEndpointMappings of the UnicastBusConfig section in your configuration file. " +
                                                           "It may also be the case that the given queue just hasn't been created yet, or has been deleted."
                                                           , ex);
                }

                if (Log.IsDebugEnabled)
                {
                    Log.Debug(string.Format("Sending message {0} with ID {1} to destination {2}.\n" +
                                            "ToString() of the message yields: {3}\n" +
                                            "Message headers:\n{4}",
                                            messages[0].GetType().AssemblyQualifiedName,
                                            toSend.Id,
                                            destination,
                                            messages[0],
                                            string.Join(", ", toSend.Headers.Select(h => h.Key + ":" + h.Value).ToArray())
                                            ));
                }

                result.Add(toSend.Id);
            }

            if (MessagesSent != null)
            {
                MessagesSent(this, new MessagesEventArgs(messages));
            }

            return(result);
        }