Ejemplo n.º 1
0
        public IDisposable SubscribeEvent(string eventCategory, Action <EventData> callback)
        {
            var subscription = new EventQueueSubscription <TContext>(this, eventCategory, callback);

            lock (this)
            {
                if (!_subscriptions.ContainsKey(eventCategory))
                {
                    _subscriptions.Add(eventCategory, new List <EventQueueSubscription <TContext> >());
                }
                _subscriptions[eventCategory].Add(subscription);
            }

            return(subscription);
        }
Ejemplo n.º 2
0
 private void InvokeCallback(EventQueueSubscription <TContext> subscription, EventData @event)
 {
     _logger.LogDebug($"Invoke callback for event -> {@event.EventCategory} {@event.EventType} {@event.EventId}");
     Task.Run(() =>
     {
         try
         {
             _logger.LogDebug($"Invoking callback for event -> {@event.EventCategory} {@event.EventType} {@event.EventId}");
             subscription.Callback(@event);
         }
         catch (Exception e)
         {
             _logger.LogError(e, $"Error invoking callback for event -> {@event.EventCategory} {@event.EventType} {@event.EventId}");
         }
     })
     .ConfigureAwait(false);
 }
Ejemplo n.º 3
0
        internal void Unsubscribe(EventQueueSubscription <TContext> subscription)
        {
            lock (this)
            {
                if (!_subscriptions.ContainsKey(subscription.EventCategory))
                {
                    throw new InvalidOperationException($"Event category subscriptions {subscription.EventCategory} not found");
                }

                var subscriptions = _subscriptions[subscription.EventCategory];
                if (!subscriptions.Contains(subscription))
                {
                    throw new InvalidOperationException($"Subscription not found");
                }

                subscriptions.Remove(subscription);
            }
        }