示例#1
0
        /// <summary>
        /// 消息分发失败处理函数
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnDispatchFailed(MessageDispatchEventArgs e)
        {
            var temp = this.DispatchFailed;

            if (temp != null)
            {
                temp(this, e);
            }
        }
示例#2
0
        /// <summary>
        /// 分发消息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="message"></param>
        public virtual void DispatchMessage <T>(T message)
        {
            Type messageType = typeof(T);

            if (_handlers.ContainsKey(messageType))
            {
                var messageHandlers = _handlers[messageType];
                foreach (var messageHandler in messageHandlers)
                {
                    var dynMessageHandler = (IHandler <T>)messageHandler;
                    var evtArgs           = new MessageDispatchEventArgs(message, messageHandler.GetType(), messageHandler);
                    this.OnDispatching(evtArgs);
                    try
                    {
                        dynMessageHandler.Handle(message);
                        this.OnDispatched(evtArgs);
                    }
                    catch
                    {
                        this.OnDispatchFailed(evtArgs);
                    }
                }
            }
        }