コード例 #1
0
        public void Raise(IDomainEvent domainEvent)
        {
            WriteLog($"Raising Event {domainEvent.GetType()}");
            // Cache event
            if (FiTechCoreExtensions.StdoutEventHubLogs)
            {
                domainEvent.d_RaiseOrigin = Environment.StackTrace;
            }
            domainEvent.EventsHub = this;
            lock (EventCache) {
                if (EventCache.Any(x => x.RID == domainEvent.RID))
                {
                    return;
                }
                EventCache.RemoveAll(e => (DateTime.UtcNow - e.TimeStamp) > EventCacheDuration);
            }

            // Raise event on all listeners.
            List <IDomainEventListener> listeners;

            lock (Listeners) {
                Listeners.RemoveAll(l => l == null);
                listeners = Listeners.ToList();
            }
            foreach (var listener in listeners)
            {
                MainQueuer.Enqueue(async() => {
                    await listener.OnEventTriggered(domainEvent).ConfigureAwait(false);
                    if (domainEvent.AllowPropagation)
                    {
                        parentHub?.Raise(domainEvent);
                    }
                }, async x => {
                    try {
                        await listener.OnEventHandlingError(domainEvent, x).ConfigureAwait(false);
                    } catch (Exception y) {
                        Fi.Tech.Throw(x);
                    }
                }, (b) => {
                    return(Fi.Result());
                });
            }

            if (EnableEventCache)
            {
                lock (EventCache) {
                    domainEvent.TimeStamp = DateTime.UtcNow;
                    EventCache.Add(domainEvent);
                }
                LastEventDateTime = Fi.Tech.GetUtcTime();
                CancelationTokenSource.Cancel();
                CancelationTokenSource = new CancellationTokenSource();
            }
        }