public Delegate PublishToDelegate(string eventId, IStateObject source)
        {
            eventId = eventId.ToUpper();
            var promisesInfo = HasSubscribers(eventId, source) as PromisesInfo;

            if (promisesInfo != null)
            {
                foreach (string eventsnameid in promisesInfo.GetListMethodsMame())
                {
                    var subscriptionId   = UniqueIDGenerator.GetEventRelativeUniqueID(source, eventsnameid);
                    var eventHandlerInfo = _stateManager.GetObject(subscriptionId) as EventPromiseInfo;
                    if (eventHandlerInfo != null)
                    {
                        var result = this.PublishInternal(eventHandlerInfo, eventId, source, true, null);
                        return(result);
                    }
                    else
                    {
                        TraceUtil.TraceError(string.Format("EventAggregator::PublishToMainSyncronizationContext. No Events found for EventId [{0}] on object [{1}]", subscriptionId, source == null ? "null" : source.UniqueID));
                    }
                }
            }
            else
            {
                TraceUtil.TraceError(string.Format("HandlersEventAggregator::PublishToMainSyncronizationContext. No Events found for EventId [{0}] on object [{1}]", eventId, source == null ? "null" : source.UniqueID));
            }
            return(null);
        }
        /// <summary>
        /// Will register a subscriber. Event will be recognized by "eventId" and will be attached to
        /// obj
        /// </summary>
        /// <param name="eventId"></param>
        /// <param name="obj"></param>
        /// <param name="handler"></param>
        public void Subscribe(string eventId, IStateObject obj, Delegate handler, out string eventSubscriptionId, bool isDynamic = false)
        {
            var result = String.Empty;

            if (handler != null)
            {
                //get/created subscribed promises info
                eventId = eventId.ToUpper();
                var handlerSubscriptionId = UniqueIDGenerator.GetEventRelativeUniqueID(obj, eventId);
                LazyBehaviours.AddDependent(obj, UniqueIDGenerator.EVENTPrefix + eventId);
                var promisesInfo = PromisesInfo.CreateInstance(handlerSubscriptionId);

                ////subscribe handler
                var eventMethodName = handler.Method.Name.ToUpper() + eventId;
                eventSubscriptionId = UniqueIDGenerator.GetEventRelativeUniqueID(obj, eventMethodName);
                LazyBehaviours.AddDependent(obj, UniqueIDGenerator.EVENTPrefix + eventMethodName);
                promisesInfo.Add(eventMethodName);
                if (isDynamic)
                {
                    EventPromiseInfoForClient.CreateEventInstance(_stateManager, handler, obj, eventSubscriptionId, actionID: eventId);
                }
                else
                {
                    EventPromiseInfo.CreateEventInstance(_stateManager, handler, obj, eventSubscriptionId);
                }
#if DEBUG
                Debug.Assert(_stateManager.GetObject(eventSubscriptionId) != null, "Event Subscription Failed", "Event for {0} on Object {1} failed", eventId, obj.UniqueID);
#endif
                result = eventSubscriptionId;
            }
            eventSubscriptionId = result;
        }
        private void PublishInternal(string eventId, IStateObject source, object[] args)
        {
            eventId = eventId.ToUpper();
            var promisesInfo = HasSubscribers(eventId, source) as PromisesInfo;

            if (promisesInfo != null)
            {
                foreach (string eventsnameid in promisesInfo.GetListMethodsMame())
                {
                    var subscriptionId   = UniqueIDGenerator.GetEventRelativeUniqueID(source, eventsnameid);
                    var eventHandlerInfo = _stateManager.GetObject(subscriptionId) as EventPromiseInfo;
                    if (eventHandlerInfo != null)
                    {
                        this.PublishInternal(eventHandlerInfo, eventId, source, false, args);
                    }
                    else
                    {
                        TraceUtil.TraceError(string.Format("EventAggregator::PublishInternal. No Events found for EventId [{0}] on object [{1}]", subscriptionId, source == null ? "null" : source.UniqueID));
                    }
                }
            }
            else
            {
                TraceUtil.TraceError(string.Format("HandlersEventAggregator::PublishInternal. No Events found for EventId [{0}] on object [{1}]", eventId, source == null ? "null" : source.UniqueID));
            }
        }
        public IStateObject HasSubscribers(string eventId, IStateObject source)
        {
            eventId = eventId.ToUpper();
            if (source == null)
            {
                TraceUtil.TraceError("EventAggregator::PublishHighPriorityEvent Error publishing event [" + eventId + "] source argument should not be null");
                return(null);
            }
            var subscriptionId = UniqueIDGenerator.GetEventRelativeUniqueID(source, eventId);
            var promisesInfo   = _stateManager.GetObject(subscriptionId) as PromisesInfo;

            return(promisesInfo);
        }
        public void UnSubscribe(string eventId, IStateObject source, string methodName)
        {
            eventId = eventId.ToUpper();
            var promisesInfo = HasSubscribers(eventId, source) as PromisesInfo;

            if (promisesInfo != null)
            {
                methodName = methodName.ToUpper();
                promisesInfo.Remove(methodName);
                var eventSubscriptionId = UniqueIDGenerator.GetEventRelativeUniqueID(source, methodName + eventId);
                _stateManager.RemoveObject(eventSubscriptionId, true);

                if (promisesInfo.IsEmpty())
                {
                    eventSubscriptionId = UniqueIDGenerator.GetEventRelativeUniqueID(source, eventId);
                    _stateManager.RemoveObject(eventSubscriptionId, true);
                }
            }
        }