Beispiel #1
0
 void RegisterLocalMessages()
 {
     TypesToScan
     .Where(t => t.IsMessageType())
     .ToList()
     .ForEach(t => typesToEndpoints[t] = Address.Undefined);
 }
        /// <summary>
        /// Wrap the given configure object storing its builder and configurer.
        /// </summary>
        /// <param name="config"></param>
        public void Configure(Configure config)
        {
            Builder    = config.Builder;
            Configurer = config.Configurer;

            busConfig = Configurer.ConfigureComponent <TpUnicastBus>(ComponentCallModelEnum.Singleton);

            ConfigureSubscriptionAuthorization();

            RegisterMessageModules();

            var cfg = GetConfigSection <UnicastBusConfig>();

            if (cfg != null)
            {
                TypesToScan.Where(t => typeof(IMessage).IsAssignableFrom(t)).ToList()
                .ForEach(t => assembliesToEndpoints[t.Assembly.GetName().Name] = string.Empty);

                foreach (MessageEndpointMapping mapping in cfg.MessageEndpointMappings)
                {
                    assembliesToEndpoints[mapping.Messages] = mapping.Endpoint;
                }

                busConfig.ConfigureProperty(b => b.DistributorControlAddress, cfg.DistributorControlAddress);
                busConfig.ConfigureProperty(b => b.DistributorDataAddress, cfg.DistributorDataAddress);
                busConfig.ConfigureProperty(b => b.ForwardReceivedMessagesTo, cfg.ForwardReceivedMessagesTo);
                busConfig.ConfigureProperty(b => b.MessageOwners, assembliesToEndpoints);
            }
        }
Beispiel #3
0
#pragma warning disable 0618
        void RegisterMessageModules()
        {
            TypesToScan
            .Where(t => typeof(IMessageModule).IsAssignableFrom(t) && !t.IsInterface)
            .ToList()
            .ForEach(type => Configurer.ConfigureComponent(type, DependencyLifecycle.InstancePerCall));
        }
Beispiel #4
0
 public void ScanTypes(IEnumerable <Type> types)
 {
     foreach (var type in types)
     {
         TypesToScan.Add(type);
     }
 }
Beispiel #5
0
        /// <summary>
        /// Scans the given types for types that are message handlers
        /// then uses the Configurer to configure them into the container as single call components,
        /// finally passing them to the bus as its MessageHandlerTypes.
        /// </summary>
        /// <param name="types"></param>
        /// <returns></returns>
        ConfigUnicastBus ConfigureMessageHandlersIn(IEnumerable <Type> types)
        {
            var handlerRegistry = new MessageHandlerRegistry();
            var handlers        = new List <Type>();

            foreach (var t in types.Where(IsMessageHandler))
            {
                Configurer.ConfigureComponent(t, DependencyLifecycle.InstancePerUnitOfWork);
                handlerRegistry.RegisterHandler(t);
                handlers.Add(t);
            }

            Configurer.RegisterSingleton <IMessageHandlerRegistry>(handlerRegistry);

            var availableDispatcherFactories = TypesToScan
                                               .Where(
                factory =>
                !factory.IsInterface && typeof(IMessageDispatcherFactory).IsAssignableFrom(factory))
                                               .ToList();

            var dispatcherMappings = GetDispatcherFactories(handlers, availableDispatcherFactories);

            //configure the message dispatcher for each handler
            busConfig.ConfigureProperty(b => b.MessageDispatcherMappings, dispatcherMappings);
            Configurer.ConfigureProperty <InvokeHandlersBehavior>(b => b.MessageDispatcherMappings, dispatcherMappings);

            availableDispatcherFactories.ToList().ForEach(factory => Configurer.ConfigureComponent(factory, DependencyLifecycle.InstancePerUnitOfWork));

            return(this);
        }
Beispiel #6
0
        /// <summary>
        ///
        /// Loads all message handler assemblies in the runtime directory.
        /// </summary>
        /// <returns></returns>
        public ConfigUnicastBus LoadMessageHandlers()
        {
            var types = new List <Type>();

            TypesToScan.Where(TypeSpecifiesMessageHandlerOrdering)
            .ToList().ForEach(t =>
            {
                Logger.DebugFormat("Going to ask for message handler ordering from {0}.", t);

                var order = new Order();
                ((ISpecifyMessageHandlerOrdering)Activator.CreateInstance(t)).SpecifyOrder(order);

                order.Types.ToList().ForEach(ht =>
                {
                    if (types.Contains(ht))
                    {
                        throw new ConfigurationErrorsException(string.Format("The order in which the type {0} should be invoked was already specified by a previous implementor of ISpecifyMessageHandlerOrdering. Check the debug logs to see which other specifiers have been invoked.", ht));
                    }
                });

                types.AddRange(order.Types);
            });

            return(LoadMessageHandlers(types));
        }
Beispiel #7
0
 private void RegisterLocalMessages()
 {
     TypesToScan
     .Where(t => typeof(IMessage).IsAssignableFrom(t))
     .ToList()
     .ForEach(t => assembliesToEndpoints[t.Assembly.GetName().Name] = string.Empty);
 }
Beispiel #8
0
 void RegisterLocalMessages()
 {
     TypesToScan
     .Where(t => t.IsMessageType())
     .ToList()
     .ForEach(t => MapTypeToAddress(t, Address.Undefined));
 }
Beispiel #9
0
        /// <summary>
        /// Configure to scan the given types.
        /// </summary>
        public static Configure With(IEnumerable <Type> typesToScan)
        {
            if (instance == null)
            {
                instance = new Configure();
            }

            TypesToScan = typesToScan.Union(GetAllowedTypes(Assembly.GetExecutingAssembly())).ToList();

            if (HttpRuntime.AppDomainAppId == null)
            {
                var baseDirectory = lastProbeDirectory ?? AppDomain.CurrentDomain.BaseDirectory;
                var hostPath      = Path.Combine(baseDirectory, "NServiceBus.Host.exe");
                if (File.Exists(hostPath))
                {
                    TypesToScan = TypesToScan.Union(GetAllowedTypes(Assembly.LoadFrom(hostPath))).ToList();
                }
            }

            //TODO: re-enable when we make message scanning lazy #1617
            //TypesToScan = TypesToScan.Union(GetMessageTypes(TypesToScan)).ToList();

            Logger.DebugFormat("Number of types to scan: {0}", TypesToScan.Count);

            EndpointHelper.StackTraceToExamine = new StackTrace();

            instance.InvokeISetDefaultSettings();

            return(instance);
        }
Beispiel #10
0
        /// <summary>
        /// Finalizes the configuration by invoking all initializers.
        /// </summary>
        public void Initialize()
        {
            if (initialized)
            {
                return;
            }

            TypesToScan.Where(t => typeof(IWantToRunWhenConfigurationIsComplete).IsAssignableFrom(t) && !(t.IsAbstract || t.IsInterface))
            .ToList().ForEach(t => Configurer.ConfigureComponent(t, DependencyLifecycle.InstancePerCall));

            TypesToScan.Where(t => typeof(IWantToRunBeforeConfiguration).IsAssignableFrom(t) && !(t.IsAbstract || t.IsInterface))
            .ToList().ForEach(t =>
            {
                var ini = (IWantToRunBeforeConfiguration)Activator.CreateInstance(t);
                ini.Init();
            });

            TypesToScan.Where(t => typeof(INeedInitialization).IsAssignableFrom(t) && !(t.IsAbstract || t.IsInterface))
            .ToList().ForEach(t =>
            {
                var ini = (INeedInitialization)Activator.CreateInstance(t);
                ini.Init();
            });

            initialized = true;

            if (ConfigurationComplete != null)
            {
                ConfigurationComplete();
            }

            Builder.BuildAll <IWantToRunWhenConfigurationIsComplete>()
            .ToList().ForEach(o => o.Run());
        }
Beispiel #11
0
 private void InvokeBeforeConfigurationInitializers()
 {
     TypesToScan.Where(t => typeof(IWantToRunBeforeConfiguration).IsAssignableFrom(t) && !(t.IsAbstract || t.IsInterface))
     .ToList().ForEach(t =>
     {
         var ini = (IWantToRunBeforeConfiguration)Activator.CreateInstance(t);
         ini.Init();
     });
 }
Beispiel #12
0
#pragma warning restore 0618

        void ConfigureSubscriptionAuthorization()
        {
            var authType = TypesToScan.FirstOrDefault(t => typeof(IAuthorizeSubscriptions).IsAssignableFrom(t) && !t.IsInterface);

            if (authType != null)
            {
                Configurer.ConfigureComponent(authType, DependencyLifecycle.SingleInstance);
            }
        }
        private void ConfigureSubscriptionAuthorization()
        {
            Type authType =
                TypesToScan.Where(t => typeof(IAuthorizeSubscriptions).IsAssignableFrom(t) && !t.IsInterface).
                FirstOrDefault();

            if (authType != null)
            {
                Configurer.ConfigureComponent(authType, ComponentCallModelEnum.Singleton);
            }
        }
Beispiel #14
0
        void WireUpConfigSectionOverrides()
        {
            if (configSectionOverridesInitialized)
            {
                return;
            }

            TypesToScan
            .Where(t => t.GetInterfaces().Any(IsGenericConfigSource))
            .ToList().ForEach(t => configurer.ConfigureComponent(t, DependencyLifecycle.InstancePerCall));

            configSectionOverridesInitialized = true;
        }
Beispiel #15
0
        public new NsbCommandBus Configure(Configure config)
        {
            Configurer = config.Configurer;
            Builder    = config.Builder;

            ExecuteCommandTimeout = TimeSpan.FromSeconds(10);
            var commandBusConfig = GetConfigSection <CommandBusConfig>();
            var commandTypes     = TypesToScan
                                   .Where(type => typeof(ICommand).IsAssignableFrom(type))
                                   .ToList();

            RegisterAssemblyCommandDestinationMappings(commandBusConfig, commandTypes);
            RegisterCommandDestinationMappings(commandBusConfig, commandTypes);

            return(new NsbCommandBus(runtime.ServiceLocator, CommandTypeToDestinationLookup, ExecuteCommandTimeout));
        }
Beispiel #16
0
        /// <summary>
        /// Wrap the given configure object storing its builder and configurer.
        /// </summary>
        /// <param name="config"></param>
        public void Configure(Configure config)
        {
            Builder    = config.Builder;
            Configurer = config.Configurer;
            busConfig  = Configurer.ConfigureComponent <UnicastBus>(DependencyLifecycle.SingleInstance)
                         .ConfigureProperty(p => p.MasterNodeAddress, config.GetMasterNodeAddress());

            var knownMessages = TypesToScan
                                .Where(MessageConventionExtensions.IsMessageType)
                                .ToList();

            ConfigureSubscriptionAuthorization();

            RegisterMessageModules();

            RegisterMessageOwnersAndBusAddress(knownMessages);

            ConfigureMessageRegistry(knownMessages);
        }
Beispiel #17
0
        /// <summary>
        /// Finalizes the configuration by invoking all initializers.
        /// </summary>
        public void Initialize()
        {
            if (initialized)
            {
                return;
            }

            TypesToScan.Where(t => typeof(INeedInitialization).IsAssignableFrom(t) && !(t.IsAbstract || t.IsInterface))
            .ToList().ForEach(t =>
            {
                var ini = (INeedInitialization)Activator.CreateInstance(t);
                ini.Init();
            });

            initialized = true;

            if (ConfigurationComplete != null)
            {
                ConfigurationComplete(null, null);
            }
        }
Beispiel #18
0
        /// <summary>
        /// Configures nServiceBus to scan the given types.
        /// </summary>
        /// <param name="typesToScan"></param>
        /// <returns></returns>
        public static Configure With(IEnumerable <Type> typesToScan)
        {
            if (instance == null)
            {
                instance = new Configure();
            }

            TypesToScan = typesToScan.Union(GetAllowedTypes(Assembly.GetExecutingAssembly()));

            if (HttpRuntime.AppDomainAppId == null)
            {
                var hostPath = Path.Combine(lastProbeDirectory ?? AppDomain.CurrentDomain.BaseDirectory, "NServiceBus.Host.exe");
                if (File.Exists(hostPath))
                {
                    TypesToScan = TypesToScan.Union(GetAllowedTypes(Assembly.LoadFrom(hostPath)));
                }
            }

            Logger.DebugFormat("Number of types to scan: {0}", TypesToScan.Count());

            return(instance);
        }
Beispiel #19
0
 /// <summary>
 /// Applies the given action to all the scanned types that can be assigned to <typeparamref name="T"/>.
 /// </summary>
 public void ForAllTypes <T>(Action <Type> action) where T : class
 {
     TypesToScan.Where(t => typeof(T).IsAssignableFrom(t) && !(t.IsAbstract || t.IsInterface))
     .ToList().ForEach(action);
 }
 private void RegisterMessageModules()
 {
     TypesToScan.Where(t => typeof(IMessageModule).IsAssignableFrom(t) && !t.IsInterface).ToList()
     .ForEach(type => Configurer.ConfigureComponent(type, ComponentCallModelEnum.Singleton));
 }
Beispiel #21
0
 private void ContainerHasBeenSet()
 {
     TypesToScan
     .Where(t => t.GetInterfaces().Any(IsGenericConfigSource))
     .ToList().ForEach(t => configurer.ConfigureComponent(t, DependencyLifecycle.InstancePerCall));
 }