Ejemplo n.º 1
0
        protected virtual bool InvokePipeline(IExecutionPipelineContext context)
        {
            var pipeline = context.Pipeline;

            if (pipeline == null)
            {
                return(false);
            }

            //计算当前管道的执行条件,如果管道的条件评估器不为空则使用管道的评估器进行验证,否则使用管道的处理程序的CanHandle方法进行验证
            var enabled = pipeline.Predication != null?pipeline.Predication.Predicate(context) : pipeline.Handler.CanHandle(context);

            if (!enabled)
            {
                return(false);
            }

            //激发“PipelineExecuting”事件
            this.OnPipelineExecuting(new ExecutionPipelineExecutingEventArgs(context));

            Stack <IExecutionFilter> stack = null;

            //调用当前管道过滤器的前半截
            if (pipeline.HasFilters)
            {
                stack = ExecutionUtility.InvokeFiltersExecuting(pipeline.Filters, filter => this.OnFilterExecuting(filter, context));
            }

            //激发“HandlerExecuting”事件
            this.OnHandlerExecuting(new ExecutionPipelineExecutingEventArgs(context));

            //调用管道处理器处理当前请求
            var isHandled = this.InvokeHandler(context);

            //设置是否处理成功的值到到上下文的扩展属性集中
            context.ExtendedProperties["__IsHandled__"] = isHandled;

            //激发“HandlerExecuted”事件
            this.OnHandlerExecuted(new ExecutionPipelineExecutedEventArgs(context));

            //调用后续管道集合
            if (context.HasChildren)
            {
                context.Result = this.InvokePipelines(context.Children, p => this.CreatePipelineContext(context, p, context.Result));
            }

            //调用当前管道过滤器的后半截
            if (stack != null && stack.Count > 0)
            {
                ExecutionUtility.InvokeFiltersExecuted(stack, filter => this.OnFilterExecuted(filter, context));
            }

            //激发“PipelineExecuted”事件
            this.OnPipelineExecuted(new ExecutionPipelineExecutedEventArgs(context));

            return(isHandled);
        }
Ejemplo n.º 2
0
        protected ExecutionResult Execute(IExecutionContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            //创建“Executing”事件的调用参数
            ExecutingEventArgs executingArgs = new ExecutingEventArgs(context);

            //激发“Executing”事件
            this.OnExecuting(executingArgs);

            if (executingArgs.Cancel)
            {
                return(new ExecutionResult(context));
            }

            //通过选择器获取当前请求对应的管道集
            var pipelines = this.SelectPipelines(context, _pipelines);

            //如果当前执行的处理管道数为零则退出
            if (pipelines == null || pipelines.Count() < 1)
            {
                return(null);
            }

            //调用执行器过滤器的前半截
            var stack = ExecutionUtility.InvokeFiltersExecuting(context.Executor.Filters, filter => this.OnFilterExecuting(filter, context));

            //执行管道集,并将其执行的最终结果保存到执行器上下文的Result属性中
            context.Result = this.InvokePipelines(pipelines, p => this.CreatePipelineContext(context, p, context.Parameter));

            //调用执行器过滤器的后半截
            ExecutionUtility.InvokeFiltersExecuted(stack, filter => this.OnFilterExecuted(filter, context));

            //激发“Executed”事件
            this.OnExecuted(new ExecutedEventArgs(context));

            return(new ExecutionResult(context));
        }