/// <summary>
        /// 根据消息类型创建消息控制器
        /// </summary>
        /// <param name="request">微信请求上下文</param>
        /// <returns>实现IWeiXinHandler微信消息处理控制器</returns>
        public IWeiXinHandler CreateWeiXinHandler(WeiXinRequest request)
        {
            IWeiXinHandler weiXinHandler = null;

            try
            {
                foreach (Type pluginType in PluginTypes)
                {
                    WeiXinHandlerType weiXinHandlerType = Activator.CreateInstance(pluginType) as WeiXinHandlerType;

                    if (weiXinHandlerType == null)
                    {
                        continue;
                    }

                    if (request.WeiXinMsgType != WeiXinMsgType.Event)
                    {
                        if (weiXinHandlerType.WeiXinMsgType == request.WeiXinMsgType)
                        {
                            weiXinHandler = weiXinHandlerType;
                            break;
                        }
                    }
                    else
                    {
                        if (weiXinHandlerType.WeiXinEventType == request.WeiXinEventType)
                        {
                            weiXinHandler = weiXinHandlerType;
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            if (weiXinHandler == null)
            {
                throw new WeiXinHandlerNotFoundException
                      {
                          ExceptionMessage = new WeiXinTextMessageEntity
                          {
                              ToUserName = request.FromUserName,
                              Content    = "控制器未找到异常:此消息类型控制器可能未实现!",
                              MsgType    = WeiXinMsgType.Text.ToString().ToLower()
                          }.GetXElement().ToString()
                      }
            }
            ;

            return(weiXinHandler);
        }

        #endregion
    }
Exemple #2
0
 /// <summary>
 /// 构造函数,把请求消息封装到微信请求/响应上下文属性
 /// </summary>
 /// <param name="requestXml">请求消息xml</param>
 public WeiXinContext(XElement requestXml)
 {
     Request  = new WeiXinRequest(requestXml);
     Response = new WeiXinResponse();
 }