コード例 #1
0
        /// <summary>
        /// 处理图片请求
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnImageRequest(RequestMessageImage requestMessage)
        {
            //一隔一返回News或Image格式
            if (GlobalMessageContext.GetMessageContext(requestMessage).RequestMessages.Count() % 2 == 0)
            {
                var responseMessage = CreateResponseMessage <ResponseMessageNews>();

                responseMessage.Articles.Add(new Article()
                {
                    Title       = "您刚才发送了图片信息",
                    Description = "您发送的图片将会显示在边上",
                    PicUrl      = requestMessage.PicUrl,
                    Url         = "http://sdk.weixin.senparc.com"
                });
                responseMessage.Articles.Add(new Article()
                {
                    Title       = "第二条",
                    Description = "第二条带连接的内容",
                    PicUrl      = requestMessage.PicUrl,
                    Url         = "http://sdk.weixin.senparc.com"
                });

                return(responseMessage);
            }
            else
            {
                var responseMessage = CreateResponseMessage <ResponseMessageImage>();
                responseMessage.Image.MediaId = requestMessage.MediaId;
                return(responseMessage);
            }
        }
コード例 #2
0
        public void MessageContextRecordLimtTest()
        {
            //测试MessageContext的数量限制

            var openId = "olPjZjsXuQPJoV0HlruZkNzKc91E";

            //创建 GlobalMessageContext 对象
            var globalMessageContext = new GlobalMessageContext <DefaultMpMessageContext, IRequestMessageBase, IResponseMessageBase>();

            globalMessageContext.Restore();//清空所有上下文

            for (int i = 0; i < 100; i++)
            {
                var xml = string.Format(string.Format(xmlTextFormat, "测试限制"), DateTimeHelper.GetUnixDateTime(SystemTime.Now.AddSeconds(i)), SystemTime.NowTicks + i);

                Init(xml);//初始化

                var timestamp = "itsafaketimestamp";
                var nonce     = "whateveryouwant";
                var signature = CheckSignature.GetSignature(timestamp, nonce, WeixinController.Token);
                var postModel = new PostModel()
                {
                    Signature = signature,
                    Timestamp = timestamp,
                    Nonce     = nonce
                };
                var actual = target.MiniPost(postModel) as FixWeixinBugWeixinResult;
                Assert.IsNotNull(actual);
                Console.WriteLine(actual.ToJson());
            }

            //获取指定 openId 的上下文信息
            var myMessageContext = globalMessageContext.GetMessageContext(openId);

            Assert.IsNotNull(myMessageContext);

            var recordCount = myMessageContext.MaxRecordCount;

            Console.WriteLine(myMessageContext.RequestMessages[0].ToJson());
            Assert.AreEqual(recordCount, myMessageContext.RequestMessages.Count);
            Assert.AreEqual(recordCount, myMessageContext.ResponseMessages.Count);
        }