コード例 #1
0
ファイル: ResponseMessageBase.cs プロジェクト: peshxx/NeuChar
        //public virtual ResponseMessageType MsgType
        //{
        //    get { return ResponseMessageType.Text; }
        //}

        /// <summary>
        /// 获取响应类型实例,并初始化
        /// <para>如果是直接调用,建议使用CreateFromRequestMessage<T>(IRequestMessageBase requestMessage)取代此方法</para>
        /// </summary>
        /// <param name="requestMessage">请求</param>
        /// <param name="msgType">响应类型</param>
        /// <returns></returns>
        //[Obsolete("建议使用CreateFromRequestMessage<T>(IRequestMessageBase requestMessage)取代此方法")]
        private static IResponseMessageBase CreateFromRequestMessage(IRequestMessageBase requestMessage, ResponseMsgType msgType, MessageEntityEnlighten enlighten)
        {
            IResponseMessageBase responseMessage = null;

            try
            {
                switch (msgType)
                {
                case ResponseMsgType.Text:
                    responseMessage = enlighten.NewResponseMessageText();    // new ResponseMessageText();
                    break;

                case ResponseMsgType.News:
                    responseMessage = enlighten.NewResponseMessageNews();    // new ResponseMessageNews();
                    break;

                case ResponseMsgType.Music:
                    responseMessage = enlighten.NewResponseMessageMusic();    // new ResponseMessageMusic();
                    break;

                case ResponseMsgType.Image:
                    responseMessage = enlighten.NewResponseMessageImage();    // new ResponseMessageImage();
                    break;

                case ResponseMsgType.Voice:
                    responseMessage = enlighten.NewResponseMessageVoice();    // new ResponseMessageVoice();
                    break;

                case ResponseMsgType.Video:
                    responseMessage = enlighten.NewResponseMessageVideo();    // new ResponseMessageVideo();
                    break;

                case ResponseMsgType.Transfer_Customer_Service:
                    responseMessage = enlighten.NewResponseMessageTransfer_Customer_Service();    // new ResponseMessageTransfer_Customer_Service();
                    break;

                case ResponseMsgType.NoResponse:
                    responseMessage = new ResponseMessageNoResponse();
                    break;

                default:
                    throw new UnknownRequestMsgTypeException(string.Format("ResponseMsgType没有为 {0} 提供对应处理程序。", msgType), new ArgumentOutOfRangeException());
                }
            }
            catch (Exception ex)
            {
                throw new MessageHandlerException("CreateFromRequestMessage过程发生异常", ex);
            }

            return(responseMessage);
        }
コード例 #2
0
ファイル: ResponseMessageBase.cs プロジェクト: peshxx/NeuChar
        /// <summary>
        /// 从返回结果XML转换成IResponseMessageBase实体类
        /// </summary>
        /// <param name="xml">返回给服务器的Response Xml</param>
        /// <returns></returns>
        public static IResponseMessageBase CreateFromResponseXml(string xml, MessageEntityEnlighten enlighten)
        {
            try
            {
                if (string.IsNullOrEmpty(xml))
                {
                    return(null);
                }

                var doc = XDocument.Parse(xml);
                IResponseMessageBase responseMessage = null;
                var msgType = (ResponseMsgType)Enum.Parse(typeof(ResponseMsgType), doc.Root.Element("MsgType").Value, true);
                switch (msgType)
                {
                case ResponseMsgType.Text:
                    responseMessage = enlighten.NewResponseMessageText();    // new ResponseMessageText();
                    break;

                case ResponseMsgType.News:
                    responseMessage = enlighten.NewResponseMessageNews();    // new ResponseMessageNews();
                    break;

                case ResponseMsgType.Music:
                    responseMessage = enlighten.NewResponseMessageMusic();    // new ResponseMessageMusic();
                    break;

                case ResponseMsgType.Image:
                    responseMessage = enlighten.NewResponseMessageImage();    // new ResponseMessageImage();
                    break;

                case ResponseMsgType.Voice:
                    responseMessage = enlighten.NewResponseMessageVoice();    // new ResponseMessageVoice();
                    break;

                case ResponseMsgType.Video:
                    responseMessage = enlighten.NewResponseMessageVideo();    // new ResponseMessageVideo();
                    break;

                case ResponseMsgType.Transfer_Customer_Service:
                    responseMessage = enlighten.NewResponseMessageTransfer_Customer_Service();    // new ResponseMessageTransfer_Customer_Service();
                    break;
                }

                (responseMessage as ResponseMessageBase).FillEntityWithXml(doc);//如果不是ResponseMessageBase会抛出异常
                return(responseMessage);
            }
            catch (Exception ex)
            {
                throw new MessageHandlerException("ResponseMessageBase.CreateFromResponseXml<T>过程发生异常!" + ex.Message, ex);
            }
        }