Example #1
0
        public void Publish(TransportMessage message, PublishOptions publishOptions)
        {
            if (SubscriptionStorage == null)
            {
                throw new InvalidOperationException("Cannot publish on this endpoint - no subscription storage has been configured.");
            }

            var eventTypesToPublish = MessageMetadataRegistry.GetMessageMetadata(publishOptions.EventType.FullName)
                                      .MessageHierarchy
                                      .Distinct()
                                      .ToList();

            var subscribers = SubscriptionStorage.GetSubscriberAddressesForMessage(eventTypesToPublish.Select(t => new MessageType(t))).ToList();

            if (!subscribers.Any())
            {
                PipelineExecutor.CurrentContext.Set("NoSubscribersFoundForMessage", true);
                return;
            }

            PipelineExecutor.CurrentContext.Set("SubscribersForEvent", subscribers);

            foreach (var subscriber in subscribers)
            {
                //this is unicast so we give the message a unique ID
                message.ChangeMessageId(CombGuid.Generate().ToString());

                MessageSender.Send(message, new SendOptions(subscriber)
                {
                    ReplyToAddress = publishOptions.ReplyToAddress,
                    EnforceMessagingBestPractices = publishOptions.EnforceMessagingBestPractices,
                    EnlistInReceiveTransaction    = publishOptions.EnlistInReceiveTransaction,
                });
            }
        }
Example #2
0
        /// <summary>
        /// Publishes the given message to all subscribers
        /// </summary>
        /// <param name="message"></param>
        /// <param name="eventTypes"></param>
        /// <returns></returns>
        public bool Publish(TransportMessage message, IEnumerable <Type> eventTypes)
        {
            if (SubscriptionStorage == null)
            {
                throw new InvalidOperationException("Cannot publish on this endpoint - no subscription storage has been configured. Add either 'MsmqSubscriptionStorage()' or 'DbSubscriptionStorage()' after 'NServiceBus.Configure.With()'.");
            }

            var subscribers = Enumerable.ToList <Address>(SubscriptionStorage.GetSubscriberAddressesForMessage(eventTypes.Select(t => new MessageType(t))));

            if (!subscribers.Any())
            {
                return(false);
            }

            foreach (var subscriber in subscribers)
            {
                //this is unicast so we give the message a unique ID
                message.ChangeMessageId(CombGuid.Generate().ToString());

                MessageSender.Send(message, subscriber);
            }

            return(true);
        }
Example #3
0
 public void Handle(ListSubscribers message)
 {
     Context.SubscribersOfTheEvent = SubscriptionStorage
                                     .GetSubscriberAddressesForMessage(new[] { new MessageType(typeof(MyEvent)) }).Select(a => a.ToString()).ToList();
     Context.Done = true;
 }