protected virtual bool CallHandler(object message, object handler, MsgHandlerInfo mhi, IMessageBus bus)
 {
     if (handler is SagaBase)
     {
         if (SagaHandler == null)
         {
             throw new PermanentMessageProcessingException("Saga support has not been enabled");
         }
         string id = null;
         if (bus != null && bus.CurrentMessageInfo != null)
         {
             id = bus.CurrentMessageInfo.CorrelationId;
         }
         bool isInitiator = mhi.InitiatedByGenericType.IsAssignableFrom(handler.GetType());
         var  b           = SagaHandler.DispatchToSaga(id, message, isInitiator, false, (SagaBase)handler, delegate(SagaBase saga)
         {
             mhi.HandleMethod(saga, message);
         });
         if (b == Sagas.SagaStateHelper.SagaDispatchResult.ConcurrentUpdateHandleLater)
         {
             //bus.HandleCurrentMessageLater(DateTime.Now.AddSeconds(1));
             //throw new Exception("Should not happen");
             throw new RetryMessageProcessingException(DateTime.Now.AddSeconds(1), "saga locked");
         }
         return(true);
     }
     else
     {
         mhi.HandleMethod(handler, message);
         return(true);
     }
 }
        protected MsgHandlerInfo GetOutHandlersFor(Type msgType)
        {
            MsgHandlerInfo mi;

            if (_outHandlers.TryGetValue(msgType, out mi))
            {
                return(mi);
            }
            lock (this)
            {
                if (_outHandlers.TryGetValue(msgType, out mi))
                {
                    return(mi);
                }
                mi             = new MsgHandlerInfo();
                mi.MessageType = msgType;
                mi.MessageHandlerGenericType = typeof(IOutgoingMessageHandler <>).MakeGenericType(msgType);
                mi.InitiatedByGenericType    = typeof(IOutgoingMessageHandler <>).MakeGenericType(msgType);
                mi.HandleMethod       = DelegateFactory.CreateMessageHandlerDelegate(mi.MessageHandlerGenericType.GetMethod("OnMessageSend"));
                _outHandlers[msgType] = mi;
                return(mi);
            }
        }
 protected virtual bool GetOutgoingHandlersForMessageType(Type t, out ICollection <object> handlers, out MsgHandlerInfo handlerInfo)
 {
     handlers    = null;
     handlerInfo = GetOutHandlersFor(t);
     if (handlerInfo == null)
     {
         return(false);
     }
     handlers = ServiceLocator.GetAllInstances(handlerInfo.MessageHandlerGenericType);
     return(true);
 }
        protected virtual bool GetAllHandlersForMessageType(Type t, out ICollection <object> handlers, out MsgHandlerInfo handlerInfo)
        {
            handlers    = null;
            handlerInfo = GetHandlersFor(t);
            if (handlerInfo == null)
            {
                return(false);
            }
            var nh = handlerInfo._numHandlersFound;

            handlers = ServiceLocator.GetAllInstances(handlerInfo.MessageHandlerGenericType);
            if (nh.HasValue)
            {
                //yeah, I know this is not thread safe but it's supposed to be a harmless sanity check
                if (nh.Value != handlers.Count)
                {
                    log.Warn("Number of handlers changed for type {0}. Was {1}, now is {2}", t.FullName, nh.Value, handlers.Count);
                    handlerInfo._numHandlersFound = handlers.Count;
                }
            }
            else
            {
                handlerInfo._numHandlersFound = handlers.Count;
            }
            return(true);
        }
 protected MsgHandlerInfo GetOutHandlersFor(Type msgType)
 {
     MsgHandlerInfo mi;
     if (_outHandlers.TryGetValue(msgType, out mi))
         return mi;
     lock (this)
     {
         if (_outHandlers.TryGetValue(msgType, out mi))
             return mi;
         mi = new MsgHandlerInfo();
         mi.MessageType = msgType;
         mi.MessageHandlerGenericType = typeof(IOutgoingMessageHandler<>).MakeGenericType(msgType);
         mi.InitiatedByGenericType = typeof(IOutgoingMessageHandler<>).MakeGenericType(msgType);
         mi.HandleMethod = DelegateFactory.CreateMessageHandlerDelegate(mi.MessageHandlerGenericType.GetMethod("OnMessageSend"));
         _outHandlers[msgType] = mi;
         return mi;
     }
 }
 protected virtual bool GetOutgoingHandlersForMessageType(Type t, out ICollection<object> handlers, out MsgHandlerInfo handlerInfo)
 {
     handlers = null;
     handlerInfo = GetOutHandlersFor(t);
     if (handlerInfo == null) return false;
     handlers = ServiceLocator.GetAllInstances(handlerInfo.MessageHandlerGenericType);
     return true;
 }
 protected virtual bool GetAllHandlersForMessageType(Type t, out ICollection<object> handlers, out MsgHandlerInfo handlerInfo)
 {
     handlers = null;
     handlerInfo = GetHandlersFor(t);
     if (handlerInfo == null) return false;
     var nh = handlerInfo._numHandlersFound;
     handlers = ServiceLocator.GetAllInstances(handlerInfo.MessageHandlerGenericType);
     if (nh.HasValue)
     {
         //yeah, I know this is not thread safe but it's supposed to be a harmless sanity check
         if (nh.Value != handlers.Count)
         {
             log.Warn("Number of handlers changed for type {0}. Was {1}, now is {2}", t.FullName, nh.Value, handlers.Count);
             handlerInfo._numHandlersFound = handlers.Count;
         }
     }
     else handlerInfo._numHandlersFound = handlers.Count;
     return true;
 }
 protected virtual bool CallHandler(object message, object handler, MsgHandlerInfo mhi, IMessageBus bus)
 {
     if (handler is SagaBase)
     {
         if (SagaHandler == null) throw new PermanentMessageProcessingException("Saga support has not been enabled");
         string id = null;
         if (bus != null && bus.CurrentMessageInfo != null)
         {
             id = bus.CurrentMessageInfo.CorrelationId;
         }
         bool isInitiator = mhi.InitiatedByGenericType.IsAssignableFrom(handler.GetType());
         var b = SagaHandler.DispatchToSaga(id, message, isInitiator, false, (SagaBase)handler, delegate(SagaBase saga)
         {
             mhi.HandleMethod(saga, message);
         });
         if (b == Sagas.SagaStateHelper.SagaDispatchResult.ConcurrentUpdateHandleLater)
         {
             //bus.HandleCurrentMessageLater(DateTime.Now.AddSeconds(1));
             //throw new Exception("Should not happen");
             throw new RetryMessageProcessingException(DateTime.Now.AddSeconds(1), "saga locked");
         }
         return true;
     }
     else
     {
         mhi.HandleMethod(handler, message);
         return true;
     }
 }