Esempio n. 1
0
 public static void AssertIsValidForReply(Type messageType)
 {
     if (MessageConventionExtensions.IsCommandType(messageType) || MessageConventionExtensions.IsEventType(messageType))
     {
         throw new InvalidOperationException("Reply is neither supported for Commands nor Events. Commands should be sent to their logical owner using bus.Send and bus. Events should be Published with bus.Publish.");
     }
 }
Esempio n. 2
0
 /// <summary>
 /// Enforce messaging rules. Make sure, the message can be used within the <see cref="IBus.Send(object[])"/>.
 /// </summary>
 /// <param name="messageType">Event, Command or message</param>
 /// <param name="messageIntent">The intent of the message</param>
 public static void AssertIsValidForSend(Type messageType, MessageIntentEnum messageIntent)
 {
     if (MessageConventionExtensions.IsEventType(messageType) && messageIntent != MessageIntentEnum.Publish)
     {
         throw new InvalidOperationException("Events can have multiple recipient so they should be published");
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Enforce messaging rules. Make sure, the message can be used by pubsub bus methods (<see cref="IBus.Subscribe(System.Type)"/>, <see cref="IBus.Unsubscribe"/> and <see cref="IBus.Publish{T}(T[])"/>)..
        /// </summary>
        public static void AssertIsValidForPubSub(Type messageType)
        {
            if (MessageConventionExtensions.IsCommandType(messageType))
            {
                throw new InvalidOperationException("Pub/Sub is not supported for Commands. They should be be sent direct to their logical owner.");
            }

            if (!MessageConventionExtensions.IsEventType(messageType))
            {
                Log.Info("You are using a basic message to do pub/sub, consider implementing the more specific ICommand and IEvent interfaces to help NServiceBus to enforce messaging best practices for you.");
            }
        }
        void RegisterMessageOwnersAndBusAddress(IEnumerable <Type> knownMessages)
        {
            var unicastConfig = Configure.GetConfigSection <UnicastBusConfig>();
            var router        = new StaticMessageRouter(knownMessages);
            var key           = typeof(DefaultAutoSubscriptionStrategy).FullName + ".SubscribePlainMessages";

            if (SettingsHolder.HasSetting(key))
            {
                router.SubscribeToPlainMessages = SettingsHolder.Get <bool>(key);
            }

            Configure.Instance.Configurer.RegisterSingleton <StaticMessageRouter>(router);

            if (unicastConfig == null)
            {
                return;
            }

            if (!string.IsNullOrWhiteSpace(unicastConfig.ForwardReceivedMessagesTo))
            {
                var forwardAddress = Address.Parse(unicastConfig.ForwardReceivedMessagesTo);
                Configure.Instance.Configurer.ConfigureProperty <UnicastBus>(b => b.ForwardReceivedMessagesTo,
                                                                             forwardAddress);
                Configure.Instance.Configurer.ConfigureProperty <ForwardBehavior>(b => b.ForwardReceivedMessagesTo, forwardAddress);
            }
            Configure.Instance.Configurer.ConfigureProperty <UnicastBus>(b => b.TimeToBeReceivedOnForwardedMessages,
                                                                         unicastConfig.TimeToBeReceivedOnForwardedMessages);
            Configure.Instance.Configurer.ConfigureProperty <ForwardBehavior>(b => b.TimeToBeReceivedOnForwardedMessages, unicastConfig.TimeToBeReceivedOnForwardedMessages);

            var messageEndpointMappings = unicastConfig.MessageEndpointMappings.Cast <MessageEndpointMapping>()
                                          .OrderByDescending(m => m)
                                          .ToList();

            foreach (var mapping in messageEndpointMappings)
            {
                mapping.Configure((messageType, address) =>
                {
                    if (!(MessageConventionExtensions.IsMessageType(messageType) || MessageConventionExtensions.IsEventType(messageType) || MessageConventionExtensions.IsCommandType(messageType)))
                    {
                        return;
                    }

                    if (MessageConventionExtensions.IsEventType(messageType))
                    {
                        router.RegisterEventRoute(messageType, address);
                        return;
                    }

                    router.RegisterMessageRoute(messageType, address);
                });
            }
        }
 public IEnumerable <Type> GetEventsToSubscribe()
 {
     return(HandlerRegistry.GetMessageTypes()
            //get all potential messages
            .Where(t => !MessageConventionExtensions.IsCommandType(t) && (SubscribePlainMessages || MessageConventionExtensions.IsEventType(t)))
            //get messages that has routing if required
            .Where(t => DoNotRequireExplicitRouting || MessageRouter.GetDestinationFor(t).Any())
            //get messages with other handlers than sagas if needed
            .Where(t => !DoNotAutoSubscribeSagas || HandlerRegistry.GetHandlerTypes(t).Any(handler => !typeof(ISaga).IsAssignableFrom(handler)))
            .ToList());
 }