예제 #1
0
        public object[] GatherConsumers(CurrentMessageInformation msg)
        {
            var message = msg.Message;

            object[] sagas       = GetSagasFor(message);
            var      sagaMessage = message as ISagaMessage;

            var msgType = message.GetType();

            object[] instanceConsumers = subscriptionStorage
                                         .GetInstanceSubscriptions(msgType);

            var consumerTypes           = reflection.GetGenericTypesOfWithBaseTypes(typeof(ConsumerOf <>), message);
            var occasionalConsumerTypes = reflection.GetGenericTypesOfWithBaseTypes(typeof(OccasionalConsumerOf <>), message);
            var consumers = GetAllNonOccasionalConsumers(consumerTypes, occasionalConsumerTypes, sagas);

            for (var i = 0; i < consumers.Length; i++)
            {
                var saga = consumers[i] as IAccessibleSaga;
                if (saga == null)
                {
                    continue;
                }

                // if there is an existing saga, we skip the new one
                var type = saga.GetType();
                if (sagas.Any(type.IsInstanceOfType))
                {
                    serviceLocator.Release(consumers[i]);
                    consumers[i] = null;
                    continue;
                }
                // we do not create new sagas if the saga is not initiated by
                // the message
                var initiatedBy = reflection.GetGenericTypeOf(typeof(InitiatedBy <>), msgType);
                if (initiatedBy.IsInstanceOfType(saga) == false)
                {
                    serviceLocator.Release(consumers[i]);
                    consumers[i] = null;
                    continue;
                }

                saga.Id = sagaMessage != null ?
                          sagaMessage.CorrelationId :
                          GuidCombGenerator.Generate();
            }
            return(instanceConsumers
                   .Union(sagas)
                   .Union(consumers.Where(x => x != null))
                   .ToArray());
        }