Exemplo n.º 1
0
        /// <summary>
        /// 当完成消息的分发后发生。
        /// </summary>
        /// <param name="e">事件数据。</param>
        protected virtual void OnDispatched(MessageDispatchEventArgs e)
        {
            var temp = this.Dispatched;

            if (temp != null)
            {
                temp(this, e);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 分发给定的消息对象。
        /// </summary>
        /// <param name="message">将被分发的消息对象。</param>
        public virtual void DispatchMessage <TMessage>(TMessage message) where TMessage : IMessage
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }
            var messageType = typeof(TMessage);

            if (!messageType.IsPublic)
            {
                messageType = messageType.BaseType;
            }
            if (messageType != null && !messageType.IsPublic)
            {
                throw new BusException("不支持分发多级继承的消息。消息的继承不应超过两级。");
            }
            if (messageType != null && _handlers.ContainsKey(messageType))
            {
                var messageHandlers = _handlers[messageType];
                foreach (var messageHandler in messageHandlers)
                {
                    var dynMessageHandler = (IHandler <TMessage>)messageHandler;
                    var evtArgs           = new MessageDispatchEventArgs(message, messageHandler.GetType(), messageHandler);
                    this.OnDispatching(evtArgs);
                    try
                    {
                        dynMessageHandler.Handle(message);
                        this.OnDispatched(evtArgs);
                    }
                    catch
                    {
                        this.OnDispatchFailed(evtArgs);
                        throw;
                    }
                }
            }
            else
            {
                // TODO:死信邮箱
            }
        }