Esempio n. 1
0
        /// <summary>
        ///     将调用转到合适的插件并由找到的插件执行处理。
        ///     <para>如果没有任何插件接收此次调用,将返回null。</para>
        /// </summary>
        /// <param name="context">如果为null,则自动从请求中获取数据</param>
        /// <returns></returns>
        public static ResponseMessage Invoke(InvokerContext context = null)
        {
            if (context == null)
            {
                context = ParseFromContext();
            }
            var msg = context.Message; //接收的消息
            var ctx = new PluginContext
            {
                ReceiveMessage = msg,
                Seller = Obtainer == null ? null : Obtainer.Obtain(msg.ToUserName),
                UserContext = UserContext.Get(msg.ToUserName, msg.FromUserName)
            };

            //需要验证签名
            if (RequireCheck && !EntrySign.ParseFromContext().Check(ctx.Seller == null ? null : ctx.Seller.Token))
            {
                return null;
            }

            var plugin = Plugins.FirstOrDefault(p => p.CanProcess(ctx));
            var rep = plugin == null ? null : plugin.Execute(ctx); //传递PluginContext

            //触发事件
            if (Invoked != null)
                Invoked(ctx, rep);

            return rep;
        }
Esempio n. 2
0
        /// <summary>
        ///     将调用转到合适的插件并由找到的插件执行处理。
        ///     <para>如果没有任何插件接收此次调用,将返回null。</para>
        /// </summary>
        /// <param name="context">如果为null,则自动从请求中获取数据</param>
        /// <returns></returns>
        public static ResponseMessage Invoke(InvokerContext context = null)
        {
            if (context == null)
            {
                context = ParseFromContext();
            }
            var msg = context.Message; //接收的消息
            var ctx = new PluginContext
            {
                ReceiveMessage = msg,
                Seller         = Obtainer == null ? null : Obtainer.Obtain(msg.ToUserName),
                UserContext    = UserContext.Get(msg.ToUserName, msg.FromUserName)
            };

            //需要验证签名
            if (RequireCheck && !EntrySign.ParseFromContext().Check(ctx.Seller == null ? null : ctx.Seller.Token))
            {
                return(null);
            }

            var plugin = Plugins.FirstOrDefault(p => p.CanProcess(ctx));
            var rep    = plugin == null ? null : plugin.Execute(ctx); //传递PluginContext

            //触发事件
            if (Invoked != null)
            {
                Invoked(ctx, rep);
            }

            return(rep);
        }