Example #1
0
 /// <summary>
 /// 条件中间件
 /// </summary>
 /// <typeparam name="TContext"></typeparam>
 /// <param name="builder"></param>
 /// <param name="predicate"></param>
 /// <param name="configureAction"></param>
 /// <returns></returns>
 public static IPipelineBuilder <TContext> When <TContext>(this IPipelineBuilder <TContext> builder, Func <TContext, bool> predicate, Action <IPipelineBuilder <TContext> > configureAction)
 {
     return(builder.Use(next => async context =>
     {
         if (predicate.Invoke(context) == true)
         {
             var branchBuilder = builder.New();
             configureAction(branchBuilder);
             await branchBuilder.Build().Invoke(context);
         }
         else
         {
             await next(context);
         }
     }));
 }
Example #2
0
        /// <summary>
        /// Conditionally creates a branch in the request pipeline that is rejoined to the main pipeline.
        /// </summary>
        /// <param name="app"></param>
        /// <param name="predicate">Invoked with the request environment to determine if the branch should be taken</param>
        /// <param name="configuration">Configures a branch to take</param>
        /// <returns></returns>
        public static IPipelineBuilder <TService, TContext> UseWhen <TService, TContext>(this IPipelineBuilder <TService, TContext> app, Predicate <TContext> predicate, Action <IPipelineBuilder <TService, TContext> > configuration)
            where TService : BaseDomainService
            where TContext : IRequestContext
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            if (predicate == null)
            {
                throw new ArgumentNullException(nameof(predicate));
            }

            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            // Create and configure the branch builder right away; otherwise,
            // we would end up running our branch after all the components
            // that were subsequently added to the main builder.
            PipelineBuilder <TService, TContext> branchBuilder = app.New();

            configuration(branchBuilder);

            return(app.Use(main =>
            {
                // This is called only when the main application builder
                // is built, not per request.
                branchBuilder.Run(main);
                RequestDelegate <TContext> branch = branchBuilder.Build();

                return context =>
                {
                    if (predicate(context))
                    {
                        return branch(context);
                    }
                    else
                    {
                        return main(context);
                    }
                };
            }));
        }
Example #3
0
        public static IPipelineBuilder <TContext> When <TContext>(this IPipelineBuilder <TContext> builder, Func <TContext, bool> predict, Action <IPipelineBuilder <TContext> > configureAction)
        {
            builder.Use((context, next) =>
            {
                if (predict.Invoke(context))
                {
                    var branchPipelineBuilder = builder.New();
                    configureAction(branchPipelineBuilder);
                    var branchPipeline = branchPipelineBuilder.Build();
                    branchPipeline.Invoke(context);
                }
                else
                {
                    next();
                }
            });

            return(builder);
        }