/// <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
    }
예제 #2
0
 /// <summary>
 /// 获取响应消息xml格式字符串
 /// </summary>
 /// <returns>响应消息xml格式字符串</returns>
 public string GetResponseXml()
 {
     try
     {
         IWeiXinHandler weiXinHandler = WeiXinHandlerFactory.GetInstance().CreateWeiXinHandler(this._context.Request);
         weiXinHandler.ProcessWeiXin(this._context);
     }
     catch (WeiXinHandlerNotFoundException ex)
     {
         throw ex;
     }
     catch (Exception ex)
     {
         throw ex;
     }
     return(this._context.Response.ResponseXml);
 }