Exemplo n.º 1
0
        /// <summary>
        /// <see cref="IBus.Unsubscribe(Type)"/>
        /// </summary>
        public virtual void Unsubscribe(Type messageType)
        {
            MessagingBestPractices.AssertIsValidForPubSub(messageType, Builder.Build <Conventions>());

            if (SendOnlyMode)
            {
                throw new InvalidOperationException("It's not allowed for a send only endpoint to unsubscribe");
            }

            AssertHasLocalAddress();


            if (SubscriptionManager == null)
            {
                throw new InvalidOperationException("No subscription manager is available");
            }

            if (TransportDefinition.HasSupportForCentralizedPubSub)
            {
                // We are dealing with a brokered transport wired for auto pub/sub.
                SubscriptionManager.Unsubscribe(messageType, null);
                return;
            }

            var addresses = GetAtLeastOneAddressForMessageType(messageType);

            foreach (var destination in addresses)
            {
                SubscriptionManager.Unsubscribe(messageType, destination);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Subscribes to receive published messages of the specified type if
        /// they meet the provided condition.
        /// </summary>
        /// <param name="messageType">The type of message to subscribe to.</param>
        /// <param name="condition">The condition under which to receive the message.</param>
        public virtual void Subscribe(Type messageType, Predicate <object> condition)
        {
            MessagingBestPractices.AssertIsValidForPubSub(messageType);

            if (Configure.SendOnlyMode)
            {
                throw new InvalidOperationException("It's not allowed for a send only endpoint to be a subscriber");
            }

            AssertHasLocalAddress();

            var destination = GetAddressForMessageType(messageType);

            if (Address.Self == destination)
            {
                throw new InvalidOperationException(string.Format("Message {0} is owned by the same endpoint that you're trying to subscribe", messageType));
            }


            if (SubscriptionManager == null)
            {
                throw new InvalidOperationException("No subscription manager is available");
            }

            SubscriptionManager.Subscribe(messageType, destination);

            if (SubscriptionPredicatesEvaluator != null)
            {
                SubscriptionPredicatesEvaluator.AddConditionForSubscriptionToMessageType(messageType, condition);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Publishes the messages to all subscribers of the first message's type.
        /// </summary>
        public virtual void Publish <T>(params T[] messages)
        {
            if (messages == null || messages.Length == 0) // Bus.Publish<IFoo>();
            {
                Publish(CreateInstance <T>(m => { }));
                return;
            }

            MessagingBestPractices.AssertIsValidForPubSub(messages[0].GetType());

            var fullTypes    = GetFullTypes(messages as object[]);
            var eventMessage = new TransportMessage {
                MessageIntent = MessageIntentEnum.Publish
            };

            MapTransportMessageFor(messages as object[], eventMessage);

            if (MessagePublisher == null)
            {
                throw new InvalidOperationException("No message publisher has been registered. If you're using a transport without native support for pub/sub please enable the message driven publishing feature by calling: Feature.Enable<MessageDrivenPublisher>() in your configuration");
            }

            var subscribersExisted = MessagePublisher.Publish(eventMessage, fullTypes);

            if (!subscribersExisted && NoSubscribersForMessage != null)
            {
                NoSubscribersForMessage(this, new MessageEventArgs(messages[0]));
            }
        }
Exemplo n.º 4
0
 public void Reply(params object[] messages)
 {
     MessagingBestPractices.AssertIsValidForReply(messages.ToList());
     if (_messageBeingHandled.ReplyToAddress == null)
     {
         throw new InvalidOperationException("Reply was called with null reply-to-address field. It can happen if you are using a SendOnly client. See http://particular.net/articles/one-way-send-only-endpoints");
     }
     SendMessage(_messageBeingHandled.ReplyToAddress, _messageBeingHandled.CorrelationId ?? _messageBeingHandled.Id, MessageIntentEnum.Reply, messages);
 }
Exemplo n.º 5
0
        /// <summary>
        /// Subscribes to receive published messages of the specified type if
        /// they meet the provided condition.
        /// </summary>
        /// <param name="messageType">The type of message to subscribe to.</param>
        /// <param name="condition">The condition under which to receive the message.</param>
        public virtual void Subscribe(Type messageType, Predicate <object> condition)
        {
            MessagingBestPractices.AssertIsValidForPubSub(messageType);

            if (Configure.SendOnlyMode)
            {
                throw new InvalidOperationException("It's not allowed for a send only endpoint to be a subscriber");
            }

            AssertHasLocalAddress();

            if (SubscriptionManager == null)
            {
                throw new InvalidOperationException("No subscription manager is available");
            }

            if (TransportDefinition.HasSupportForCentralizedPubSub && !IsAzureTransport())
            {
                // We are dealing with a brokered transport wired for auto pub/sub.
                SubscriptionManager.Subscribe(messageType, null);
                return;
            }

            var addresses = GetAddressForMessageType(messageType);

            if (addresses.Count == 0)
            {
                throw new InvalidOperationException(string.Format("No destination could be found for message type {0}. Check the <MessageEndpointMappings> section of the configuration of this endpoint for an entry either for this specific message type or for its assembly.", messageType));
            }

            foreach (var destination in addresses)
            {
                if (Address.Self == destination)
                {
                    throw new InvalidOperationException(string.Format("Message {0} is owned by the same endpoint that you're trying to subscribe", messageType));
                }

                SubscriptionManager.Subscribe(messageType, destination);

                if (SubscriptionPredicatesEvaluator != null)
                {
                    SubscriptionPredicatesEvaluator.AddConditionForSubscriptionToMessageType(messageType, condition);
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Unsubscribes from receiving published messages of the specified type.
        /// </summary>
        public virtual void Unsubscribe(Type messageType)
        {
            MessagingBestPractices.AssertIsValidForPubSub(messageType);

            if (Configure.SendOnlyMode)
            {
                throw new InvalidOperationException("It's not allowed for a send only endpoint to unsubscribe");
            }

            AssertHasLocalAddress();

            var destination = GetAddressForMessageType(messageType);

            if (SubscriptionManager == null)
            {
                throw new InvalidOperationException("No subscription manager is available");
            }

            SubscriptionManager.Unsubscribe(messageType, destination);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Unsubscribes from receiving published messages of the specified type.
        /// </summary>
        public virtual void Unsubscribe(Type messageType)
        {
            MessagingBestPractices.AssertIsValidForPubSub(messageType);

            if (Configure.SendOnlyMode)
            {
                throw new InvalidOperationException("It's not allowed for a send only endpoint to unsubscribe");
            }

            AssertHasLocalAddress();


            if (SubscriptionManager == null)
            {
                throw new InvalidOperationException("No subscription manager is available");
            }

            if (TransportDefinition.HasSupportForCentralizedPubSub)
            {
                // We are dealing with a brokered transport wired for auto pub/sub.
                SubscriptionManager.Unsubscribe(messageType, null);
                return;
            }

            var addresses = GetAddressForMessageType(messageType);

            if (addresses.Count == 0)
            {
                throw new InvalidOperationException(string.Format("No destination could be found for message type {0}. Check the <MessageEndpointMappings> section of the configuration of this endpoint for an entry either for this specific message type or for its assembly.", messageType));
            }

            foreach (var destination in addresses)
            {
                SubscriptionManager.Unsubscribe(messageType, destination);
            }
        }
Exemplo n.º 8
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);
        }