Skip to content

Senparc.Weixin.MP v9.3升级说明

JeffreySu edited this page Oct 7, 2014 · 12 revisions

Senparc.Weixin.MP v9.3 已经重写了部分MessageHandler相关代码,原先自定义一个CustomMessageContext的过程如下:

using Senparc.Weixin.Context;
using Senparc.Weixin.MP.Entities;
namespace Senparc.Weixin.MP.Sample.CommonService.CustomMessageHandler
{
    public class CustomMessageContext : MessageContext
    {
public CustomMessageContext()
        {
            base.MessageContextRemoved += CustomMessageContext_MessageContextRemoved;
        }

        void CustomMessageContext_MessageContextRemoved(object sender, Senparc.Weixin.Context.WeixinContextRemovedEventArgs e)
        {
            /* 注意,这个事件不是实时触发的(当然你也可以专门写一个线程监控)
             * 为了提高效率,根据WeixinContext中的算法,这里的过期消息会在过期后下一条请求执行之前被清除
             */

            var messageContext = e.MessageContext as CustomMessageContext;
            if (messageContext == null)
            {
                return;//如果是正常的调用,messageContext不会为null
            }

            //TODO:这里根据需要执行消息过期时候的逻辑,下面的代码仅供参考

            //Log.InfoFormat("{0}的消息上下文已过期",e.OpenId);
            //api.SendMessage(e.OpenId, "由于长时间未搭理客服,您的客服状态已退出!");
        }
    }
}

现在因为IMessageHandler接口已经分离到Senparc.Weixin.dll中,所以需要确定MessageContext对应的IRequestMessageBase和IResponseMessageBase类型(MP和QY是不同的),因此需要加上泛型指定:

using Senparc.Weixin.Context;
using Senparc.Weixin.MP.Entities;
namespace Senparc.Weixin.MP.Sample.CommonService.CustomMessageHandler
{
    public class CustomMessageContext : MessageContext<IRequestMessageBase,IResponseMessageBase>
    {
        //...
        void CustomMessageContext_MessageContextRemoved(object sender, Senparc.Weixin.Context.WeixinContextRemovedEventArgs<IRequestMessageBase,IResponseMessageBase> e)
        {
            //...
        }
        //...
    }
}

所有的修改也就是添加两处<IRequestMessageBase,IResponseMessageBase>

MessageContext<IRequestMessageBase,IResponseMessageBase>
WeixinContextRemovedEventArgs<IRequestMessageBase,IResponseMessageBase>

具体Demo可以看CustomMessageContext.cs

企业号(Senparc.Weixin.QY)对应的处理方法一致。