Esempio n. 1
0
        /// <summary>
        /// Registers all consumers in passed assembly. The actual Subscriber instances is
        /// created using <seealso cref="AutoSubscriberMessageDispatcher"/>.
        /// </summary>
        /// <param name="assemblies">The assembleis to scan for consumers.</param>
        /// <param name="configuration"></param>
        public virtual void Subscribe(List <Assembly> assemblies, Action <IAutoSubscriptionConfiguration> configuration)
        {
            Preconditions.CheckAny(assemblies, "assemblies", "No assemblies specified.");

            var autoSubscriptionConfiguration = new AutoSubscriptionConfiguration();

            configuration(autoSubscriptionConfiguration);

            var genericBusSubscribeMethod = this.GetSubscribeMethodOfBus("Subscribe", typeof(Action <>));
            var subscriptionInfos         = this.GetConsumerInfos(assemblies.SelectMany(a => a.GetTypes()), typeof(IConsume <>));

            this.InvokeMethods(
                autoSubscriptionConfiguration,
                subscriptionInfos,
                DispatchMethodName,
                genericBusSubscribeMethod,
                messageType => typeof(Action <>).MakeGenericType(messageType));

            var genericBusRespondMethod = this.GetRespondMethodOfBus("Respond", typeof(Func <,>));
            var responderInfos          = this.GetResponderInfos(assemblies.SelectMany(a => a.GetTypes()), typeof(IRespond <,>));

            this.InvokeMethods(
                autoSubscriptionConfiguration,
                responderInfos,
                HandleMethodName,
                genericBusRespondMethod,
                (messageType, responseType) => typeof(Func <,>).MakeGenericType(messageType, responseType));
        }
Esempio n. 2
0
        /// <summary>
        /// Registers all async consumers in passed assembly. The actual Subscriber instances is
        /// created using <seealso cref="AutoSubscriberMessageDispatcher"/>.
        /// </summary>
        /// <param name="assemblies">The assembleis to scan for consumers.</param>
        /// <param name="configuration"></param>
        public virtual void SubscribeAsync(List <Assembly> assemblies, Action <IAutoSubscriptionConfiguration> configuration)
        {
            Preconditions.CheckAny(assemblies, "assemblies", "No assemblies specified.");

            var autoSubscriptionConfiguration = new AutoSubscriptionConfiguration();

            configuration(autoSubscriptionConfiguration);

            var genericBusSubscribeMethod = this.GetSubscribeMethodOfBus("SubscribeAsync", typeof(Func <,>));
            var consumerInfos             = this.GetConsumerInfos(assemblies.SelectMany(a => a.GetTypes()), typeof(IConsumeAsync <>));
            Func <Type, Type> subscriberTypeFromMessageTypeDelegate =
                messageType => typeof(Func <,>).MakeGenericType(messageType, typeof(Task));

            this.InvokeMethods(
                autoSubscriptionConfiguration,
                consumerInfos,
                DispatchAsyncMethodName,
                genericBusSubscribeMethod,
                subscriberTypeFromMessageTypeDelegate);

            var genericBusRespondMethod = this.GetRespondMethodOfBus("RespondAsync", typeof(Func <,>));
            var responderInfos          = this.GetResponderInfos(assemblies.SelectMany(a => a.GetTypes()), typeof(IRespondAsync <,>));
            Func <Type, Type, Type> subscriberTypeFromMessageAndResponseTypeDelegate =
                (messageType, responseType) => typeof(Func <,>).MakeGenericType(messageType, typeof(Task <>).MakeGenericType(responseType));

            this.InvokeMethods(
                autoSubscriptionConfiguration,
                responderInfos,
                HandleAsyncMethodName,
                genericBusRespondMethod,
                subscriberTypeFromMessageAndResponseTypeDelegate);
        }
Esempio n. 3
0
        /// <summary>
        /// Registers all consumers in passed assembly. The actual Subscriber instances is
        /// created using <seealso cref="AutoSubscriberMessageDispatcher"/>.
        /// </summary>
        /// <param name="assemblies">The assembleis to scan for consumers.</param>
        /// <param name="configuration"></param>
        public virtual void Subscribe(List<Assembly> assemblies, Action<IAutoSubscriptionConfiguration> configuration)
        {
            Preconditions.CheckAny(assemblies, "assemblies", "No assemblies specified.");

            var autoSubscriptionConfiguration = new AutoSubscriptionConfiguration();
            configuration(autoSubscriptionConfiguration);

            var genericBusSubscribeMethod = this.GetSubscribeMethodOfBus("Subscribe", typeof(Action<>));
            var subscriptionInfos = this.GetConsumerInfos(assemblies.SelectMany(a => a.GetTypes()), typeof(IConsume<>));

            this.InvokeMethods(
                autoSubscriptionConfiguration,
                subscriptionInfos,
                DispatchMethodName,
                genericBusSubscribeMethod,
                messageType => typeof(Action<>).MakeGenericType(messageType));

            var genericBusRespondMethod = this.GetRespondMethodOfBus("Respond", typeof(Func<,>));
            var responderInfos = this.GetResponderInfos(assemblies.SelectMany(a => a.GetTypes()), typeof(IRespond<,>));

            this.InvokeMethods(
                autoSubscriptionConfiguration,
                responderInfos,
                HandleMethodName,
                genericBusRespondMethod,
                (messageType, responseType) => typeof(Func<,>).MakeGenericType(messageType, responseType));
        }
Esempio n. 4
0
        protected void InvokeMethods(
            AutoSubscriptionConfiguration autoSubscriptionConfiguration,
            IEnumerable <KeyValuePair <Type, AutoSubscriberConsumerInfo[]> > subscriptionInfos,
            string dispatchName,
            MethodInfo genericBusSubscribeMethod,
            Func <Type, Type> subscriberTypeFromMessageTypeDelegate)
        {
            foreach (var kv in subscriptionInfos)
            {
                foreach (var subscriptionInfo in kv.Value)
                {
                    var dispatchMethod =
                        this.AutoSubscriberMessageDispatcher.GetType()
                        .GetMethod(dispatchName, BindingFlags.Instance | BindingFlags.Public)
                        .MakeGenericMethod(subscriptionInfo.MessageType, subscriptionInfo.ConcreteType);

                    var configurationActions  = new List <Action <ISubscriptionConfiguration> >();
                    var subscriptionAttribute = this.GetSubscriptionAttribute(subscriptionInfo);
                    if (subscriptionAttribute != null)
                    {
                        configurationActions.Add(c => c.WithSubscription(subscriptionAttribute.Name));
                    }

                    var readAndDeleteAttribute = this.GetReadAndDeleteAttribute(subscriptionInfo);
                    if (readAndDeleteAttribute != null)
                    {
                        configurationActions.Add(c => c.InReadAndDeleteMode());
                    }

                    var configuration =
                        new Action <ISubscriptionConfiguration>(c => configurationActions.ForEach(o => o(c)));

                    var dispatchDelegate =
                        Delegate.CreateDelegate(
                            subscriberTypeFromMessageTypeDelegate(subscriptionInfo.MessageType),
                            this.AutoSubscriberMessageDispatcher,
                            dispatchMethod);

                    var busSubscribeMethod = genericBusSubscribeMethod.MakeGenericMethod(subscriptionInfo.MessageType);

                    busSubscribeMethod.Invoke(this.Bus, new object[] { dispatchDelegate, configuration });
                }
            }
        }
Esempio n. 5
0
        protected void InvokeMethods(
            AutoSubscriptionConfiguration autoSubscriptionConfiguration,
            IEnumerable <KeyValuePair <Type, AutoSubscriberResponderInfo[]> > subscriptionInfos,
            string handlerName,
            MethodInfo genericBusRespondMethod,
            Func <Type, Type, Type> subscriberTypeFromMessageTypeDelegate)
        {
            foreach (var kv in subscriptionInfos)
            {
                foreach (var subscriptionInfo in kv.Value)
                {
                    var configuration = new Action <IRespondConfiguration>(c => { });
                    if (autoSubscriptionConfiguration.AffinityResolver != null)
                    {
                        configuration = c => c.WithAffinityResolver(autoSubscriptionConfiguration.AffinityResolver);
                    }

                    var handleMethod =
                        this.AutoSubscriberMessageDispatcher.GetType()
                        .GetMethod(handlerName, BindingFlags.Instance | BindingFlags.Public)
                        .MakeGenericMethod(
                            subscriptionInfo.MessageType,
                            subscriptionInfo.ResponseType,
                            subscriptionInfo.ConcreteType);

                    var handleDelegate =
                        Delegate.CreateDelegate(
                            subscriberTypeFromMessageTypeDelegate(
                                subscriptionInfo.MessageType,
                                subscriptionInfo.ResponseType),
                            this.AutoSubscriberMessageDispatcher,
                            handleMethod);

                    var busRespondMethod = genericBusRespondMethod.MakeGenericMethod(
                        subscriptionInfo.MessageType,
                        subscriptionInfo.ResponseType);

                    busRespondMethod.Invoke(this.Bus, new object[] { handleDelegate, configuration });
                }
            }
        }
Esempio n. 6
0
        protected void InvokeMethods(
            AutoSubscriptionConfiguration autoSubscriptionConfiguration,
            IEnumerable<KeyValuePair<Type, AutoSubscriberResponderInfo[]>> subscriptionInfos,
            string handlerName,
            MethodInfo genericBusRespondMethod,
            Func<Type, Type, Type> subscriberTypeFromMessageTypeDelegate)
        {
            foreach (var kv in subscriptionInfos)
            {
                foreach (var subscriptionInfo in kv.Value)
                {
                    var configuration = new Action<IRespondConfiguration>(c => { });
                    if (autoSubscriptionConfiguration.AffinityResolver != null)
                    {
                        configuration = c => c.WithAffinityResolver(autoSubscriptionConfiguration.AffinityResolver);
                    }

                    var handleMethod =
                        this.AutoSubscriberMessageDispatcher.GetType()
                            .GetMethod(handlerName, BindingFlags.Instance | BindingFlags.Public)
                            .MakeGenericMethod(
                                subscriptionInfo.MessageType,
                                subscriptionInfo.ResponseType,
                                subscriptionInfo.ConcreteType);

                    var handleDelegate =
                        Delegate.CreateDelegate(
                            subscriberTypeFromMessageTypeDelegate(
                                subscriptionInfo.MessageType,
                                subscriptionInfo.ResponseType),
                            this.AutoSubscriberMessageDispatcher,
                            handleMethod);

                    var busRespondMethod = genericBusRespondMethod.MakeGenericMethod(
                        subscriptionInfo.MessageType,
                        subscriptionInfo.ResponseType);

                    busRespondMethod.Invoke(this.Bus, new object[] { handleDelegate, configuration });
                }
            }
        }
Esempio n. 7
0
        protected void InvokeMethods(
            AutoSubscriptionConfiguration autoSubscriptionConfiguration,
            IEnumerable<KeyValuePair<Type, AutoSubscriberConsumerInfo[]>> subscriptionInfos,
            string dispatchName,
            MethodInfo genericBusSubscribeMethod,
            Func<Type, Type> subscriberTypeFromMessageTypeDelegate)
        {
            foreach (var kv in subscriptionInfos)
            {
                foreach (var subscriptionInfo in kv.Value)
                {
                    var dispatchMethod =
                            this.AutoSubscriberMessageDispatcher.GetType()
                                                           .GetMethod(dispatchName, BindingFlags.Instance | BindingFlags.Public)
                                                           .MakeGenericMethod(subscriptionInfo.MessageType, subscriptionInfo.ConcreteType);

                    var configurationActions = new List<Action<ISubscriptionConfiguration>>();
                    var subscriptionAttribute = this.GetSubscriptionAttribute(subscriptionInfo);
                    if (subscriptionAttribute != null)
                    {
                        configurationActions.Add(c => c.WithSubscription(subscriptionAttribute.Name));
                    }

                    var readAndDeleteAttribute = this.GetReadAndDeleteAttribute(subscriptionInfo);
                    if (readAndDeleteAttribute != null)
                    {
                        configurationActions.Add(c => c.InReadAndDeleteMode());
                    }

                    var configuration =
                        new Action<ISubscriptionConfiguration>(c => configurationActions.ForEach(o => o(c)));

                    var dispatchDelegate =
                        Delegate.CreateDelegate(
                            subscriberTypeFromMessageTypeDelegate(subscriptionInfo.MessageType),
                            this.AutoSubscriberMessageDispatcher,
                            dispatchMethod);

                    var busSubscribeMethod = genericBusSubscribeMethod.MakeGenericMethod(subscriptionInfo.MessageType);

                    busSubscribeMethod.Invoke(this.Bus, new object[] { dispatchDelegate, configuration });
                }
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Registers all async consumers in passed assembly. The actual Subscriber instances is
        /// created using <seealso cref="AutoSubscriberMessageDispatcher"/>.
        /// </summary>
        /// <param name="assemblies">The assembleis to scan for consumers.</param>
        /// <param name="configuration"></param>
        public virtual void SubscribeAsync(List<Assembly> assemblies, Action<IAutoSubscriptionConfiguration> configuration)
        {
            Preconditions.CheckAny(assemblies, "assemblies", "No assemblies specified.");

            var autoSubscriptionConfiguration = new AutoSubscriptionConfiguration();
            configuration(autoSubscriptionConfiguration);

            var genericBusSubscribeMethod = this.GetSubscribeMethodOfBus("SubscribeAsync", typeof(Func<,>));
            var consumerInfos = this.GetConsumerInfos(assemblies.SelectMany(a => a.GetTypes()), typeof(IConsumeAsync<>));
            Func<Type, Type> subscriberTypeFromMessageTypeDelegate =
                messageType => typeof(Func<,>).MakeGenericType(messageType, typeof(Task));

            this.InvokeMethods(
                autoSubscriptionConfiguration,
                consumerInfos,
                DispatchAsyncMethodName,
                genericBusSubscribeMethod,
                subscriberTypeFromMessageTypeDelegate);

            var genericBusRespondMethod = this.GetRespondMethodOfBus("RespondAsync", typeof(Func<,>));
            var responderInfos = this.GetResponderInfos(assemblies.SelectMany(a => a.GetTypes()), typeof(IRespondAsync<,>));
            Func<Type, Type, Type> subscriberTypeFromMessageAndResponseTypeDelegate =
                (messageType, responseType) => typeof(Func<,>).MakeGenericType(messageType, typeof(Task<>).MakeGenericType(responseType));

            this.InvokeMethods(
                autoSubscriptionConfiguration,
               responderInfos,
               HandleAsyncMethodName,
               genericBusRespondMethod,
               subscriberTypeFromMessageAndResponseTypeDelegate);
        }