Beispiel #1
0
        /// <summary>
        /// Добавляет подписку.
        /// </summary>
        public MessageBusSubscriber AddSubscription(string key, Action <string, string> handler)
        {
            ConcurrentDictionary <MessageBusSubscriber, MessageBusSubscriber> keySubscriptions;

            if (!_subscriptions.TryGetValue(key, out keySubscriptions))
            {
                keySubscriptions = _subscriptions.GetOrAdd(key, new ConcurrentDictionary <MessageBusSubscriber, MessageBusSubscriber>());
            }

            MessageBusSubscriber subscriber = null;

            // ReSharper disable AccessToModifiedClosure

            subscriber = new MessageBusSubscriber(handler, () => RemoveSubscription(keySubscriptions, subscriber));

            // ReSharper restore AccessToModifiedClosure

            _syncKeySubscriptions.EnterWriteLock();

            try
            {
                keySubscriptions.TryAdd(subscriber, subscriber);
            }
            finally
            {
                _syncKeySubscriptions.ExitWriteLock();
            }

            return(subscriber);
        }
Beispiel #2
0
        private void RemoveSubscription(ConcurrentDictionary <MessageBusSubscriber, MessageBusSubscriber> keySubscriptions, MessageBusSubscriber subscriber)
        {
            _syncKeySubscriptions.EnterWriteLock();

            try
            {
                keySubscriptions.TryRemove(subscriber, out subscriber);
            }
            finally
            {
                _syncKeySubscriptions.ExitWriteLock();
            }
        }