/// <summary>
 /// Unsubscribes to the specified message type.
 /// </summary>
 /// <param name="messageType">The message type to unsubscribe.</param>
 /// <param name="subscriber">A reference to the subscriber to unsubscribe.</param>
 public void Unsubscribe(string messageType, SubscriberBase subscriber)
 {
     if (subscriberDictionary.ContainsKey(messageType))
     {
         lock (subscriberDictionary)
         {
             List <SubscriberBase> subscriberCollection = subscriberDictionary[messageType];
             subscriberCollection.Remove(subscriber);
         }
     }
 }
        private static void InvokeSubscriber(MultiPartMessage msg, SubscriberBase subscriber)
        {
            if ((MessageEngine.Instance.Tracing.Switch.Level & SourceLevels.Verbose) == SourceLevels.Verbose)
            {
                MessageEngine.Instance.Tracing.TraceEvent(TraceEventType.Verbose, 0, "Invoking subscriber: \"{0}\"...", subscriber.GetType().Name);
            }

            try
            {
                subscriber.InternalInvoke(msg);
            }
            catch (Exception ex)
            {
                if (ExceptionHelper.IsCritical(ex))
                {
                    throw;
                }
                else
                {
                    try
                    {
                        if ((MessageEngine.Instance.Tracing.Switch.Level & SourceLevels.Error) == SourceLevels.Error)
                        {
                            MessageEngine.Instance.Tracing.TraceData(TraceEventType.Error, 0, ex);
                        }

                        if ((MessageEngine.Instance.Tracing.Switch.Level & SourceLevels.Error) == SourceLevels.Error)
                        {
                            MessageEngine.Instance.Tracing.TraceData(TraceEventType.Error, 0, string.Format("Message content:\n{0}", msg.ToXmlString()));
                        }
                    }
                    catch (Exception logEx)
                    {
                        if (ExceptionHelper.IsCritical(logEx))
                        {
                            throw;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Subscribes to the specified message type.
        /// </summary>
        /// <param name="messageType">The message type to subscribe to.</param>
        /// <param name="subscriber">A reference to the subscriber.</param>
        public void Subscribe(string messageType, SubscriberBase subscriber)
        {
            if (subscriber == null)
            {
                throw new ArgumentNullException("subscriber");
            }

            if (!subscriberDictionary.ContainsKey(messageType))
            {
                lock (subscriberDictionary)
                {
                    if ((MessageEngine.Instance.Tracing.Switch.Level & SourceLevels.Information) == SourceLevels.Information)
                    {
                        MessageEngine.Instance.Tracing.TraceEvent(TraceEventType.Information, 0, "Adding subscription for: \"{0}\" to: \"{1}\".", messageType, subscriber.GetType().Name);
                    }

                    subscriberDictionary.Add(messageType, new List <SubscriberBase>());
                }
            }

            List <SubscriberBase> subscriberCollection = subscriberDictionary[messageType];

            subscriberCollection.Add(subscriber);
        }
 /// <summary>
 /// Unsubscribes to all message types.
 /// </summary>
 /// <param name="subscriber">A reference to the subscriber to unsubscribe.</param>
 public void Unsubscribe(SubscriberBase subscriber)
 {
     Unsubscribe(allMessageTypes, subscriber);
 }