Exemplo n.º 1
0
        private static IBusSubscriber SubscribeAllMessages <TMessage>
            (this IBusSubscriber subscriber, string subscribeMethod, bool excludeOperationsMessages = false, params Assembly[] assemblies)
        {
            var ass = new List <Assembly>(assemblies);

            //ass.Insert(0, MessagesAssembly);
            ass.ForEach(assembly =>
            {
                var messageTypes = assembly
                                   .GetTypes()
                                   .Where(t => t.IsClass && !(t.IsAbstract) && typeof(TMessage).IsAssignableFrom(t))
                                   .Where(t => excludeOperationsMessages && !ExcludedMessages.Contains(t))
                                   .ToList();

                var m0 = subscriber.GetType().GetMethod(subscribeMethod);
                var m1 = typeof(Extensions).GetMethod("CreateOnRejected", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
                foreach (var mt in messageTypes)
                {
                    // if (mt.Name == "CreateTypeDePropriete")
                    //   Debugger.Break();
                    try
                    {
                        var keyType = mt.GetProperty("Id").GetGetMethod().ReturnType;
                        var m2      = m0.MakeGenericMethod(mt);
                        var p0      = mt.GetCustomAttribute <MessageNamespaceAttribute>()?.Namespace;
                        var m3      = m1.MakeGenericMethod(keyType);
                        var p1      = m3.Invoke(null, new object[] { mt });
                        var p       = new object[] { p0, null, p1 };
                        m2.Invoke(subscriber, p);
                    }catch (Exception e)
                    {
                        Logger.LogError(e.Message);
#if DEBUG
                        Debugger.Break();
#endif
                    }
                }

                /*messageTypes.ForEach(mt =>
                 *  subscriber.GetType()
                 *  .GetMethod(subscribeMethod)
                 *  .MakeGenericMethod(mt)
                 *  .Invoke(subscriber,
                 *      new object[] {
                 *          mt.GetCustomAttribute<MessageNamespaceAttribute>()?.Namespace,
                 *          null,
                 *          CreateOnRejected(mt) }));*/
            });


            return(subscriber);
        }
Exemplo n.º 2
0
        private static void SubscribeCommands(IBusSubscriber subscriber, IEnumerable <ICommand> messages)
        {
            const string methodName = nameof(IBusSubscriber.Subscribe);

            foreach (var message in messages)
            {
                var subscribeMethod = subscriber.GetType().GetMethod(methodName);

                Task Handle(IServiceProvider sp, ICommand command, ICorrelationContext ctx) =>
                sp.GetService <ICommandHandler <ICommand> >().HandleAsync(command);

                subscribeMethod.MakeGenericMethod(message.GetType()).Invoke(subscriber,
                                                                            new object[] { (Func <IServiceProvider, ICommand, ICorrelationContext, Task>)Handle });
            }
        }
Exemplo n.º 3
0
        private static IBusSubscriber SubscribeAllMessages <TMessage>(this IBusSubscriber subscriber, string subscriberMethod)
        {
            var messageTypes = MessageAssembly
                               .GetTypes()
                               .Where(t => t.IsClass && typeof(TMessage).IsAssignableFrom(t))
                               .Where(t => !ExcludeMessages.Contains(t))
                               .ToList();

            messageTypes.ForEach(mt => subscriber.GetType()
                                 .GetMethod(subscriberMethod)
                                 .MakeGenericMethod(mt)
                                 .Invoke(subscriber,
                                         new object[] { mt.GetCustomAttribute <MessageNamespaceAttribute>()?.Namespace, null, null }));
            return(subscriber);
        }
        private static void SubscribeCommands(IBusSubscriber subscriber, IEnumerable <ICommand> messages)
        {
            if (messages is null)
            {
                return;
            }

            var subscribeMethod = subscriber.GetType().GetMethod(nameof(IBusSubscriber.Subscribe));

            if (subscribeMethod is null)
            {
                return;
            }

            foreach (var message in messages)
            {
                subscribeMethod.MakeGenericMethod(message.GetType()).Invoke(subscriber,
                                                                            new object[] { (Func <IServiceProvider, ICommand, object, Task>)Handle });
            }