WeChat public platform development: request message deduplication

Source: Internet
Author: User
To ensure the arrival rate of information request messages, the server will send the same request message (RequestMessage) multiple times without receiving the response message in time ), all text content, including MsgId, is consistent. To ensure the arrival rate of information request messages, the server will send the same request message (RequestMessage) multiple times without receiving the response message in time ), all text content, including MsgId, is consistent.

This mechanism ensures the success rate of message response in poor network conditions, but sometimes it takes several seconds to complete the request process due to the server load, repeated messages become a burden on the server, and may even affect services and data.

In this case, the SDK adds the deduplication setting. you only need to add one sentence when using MessageHandler:

MessageHandler. OmitRepeatedMessage = true; // enable message deduplication
////// The most simplified process (not encrypted )///[HttpPost] [ActionName ("MiniPost")] public ActionResult MiniPost (string signature, string timestamp, string nonce, string echostr) {if (! CheckSignature. Check (signature, timestamp, nonce, Token) {return new WeixinResult ("Parameter error! "); // V0.8 +} var messageHandler = new CustomMessageHandler (Request. inputStream, null, 10); messageHandler. omitRepeatedMessage = true; // enable messageHandler for message deduplication. execute (); // return new FixWeixinBugWeixinResult (messageHandler );}

The principle of deduplication is to use the context of the current user to determine whether the current request message is consistent with the MsgId of the previous request message. if it persists, the next execution is terminated.

File: Senparc. Weixin. MessageHandlers. MessageHandler. cs

Public virtual void OnExecuting () {if (OmitRepeatedMessage & CurrentMessageContext. requestMessages. count> 1) {var lastMessage = CurrentMessageContext. requestMessages [CurrentMessageContext. requestMessages. count-2]; if (lastMessage. msgId! = 0 & lastMessage. MsgId = RequestMessage. MsgId) {CancelExcute = true; // duplicate message, cancel execution }}}


For more public platform development: For more articles about message deduplication, refer to PHP!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.