コード例 #1
0
        /// <summary>
        /// Branches the request pipeline based on the async result of the given predicate.
        /// </summary>
        /// <param name="app"></param>
        /// <param name="predicate">Invoked asynchronously 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 IAppBuilder MapWhenAsync(this IAppBuilder app, PredicateAsync predicate, Action <IAppBuilder> configuration)
        {
            if (app == null)
            {
                throw new ArgumentNullException("app");
            }
            if (predicate == null)
            {
                throw new ArgumentNullException("predicate");
            }
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            // put middleware in pipeline before creating branch
            var options = new MapWhenOptions {
                PredicateAsync = predicate
            };
            IAppBuilder result = app.Use <MapWhenMiddleware>(options);

            // create branch and assign to options
            IAppBuilder branch = app.New();

            configuration(branch);
            options.Branch = (OwinMiddleware)branch.Build(typeof(OwinMiddleware));

            return(result);
        }
コード例 #2
0
        /// <summary>
        /// Branches the request pipeline based on the async result of the given predicate.
        /// </summary>
        /// <param name="app"></param>
        /// <param name="predicate">Invoked asynchronously 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 IAppBuilder MapWhenAsync(this IAppBuilder app, PredicateAsync predicate, Action<IAppBuilder> configuration)
        {
            if (app == null)
            {
                throw new ArgumentNullException("app");
            }
            if (predicate == null)
            {
                throw new ArgumentNullException("predicate");
            }
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            // put middleware in pipeline before creating branch
            var options = new MapWhenOptions { PredicateAsync = predicate };
            IAppBuilder result = app.Use<MapWhenMiddleware>(options);

            // create branch and assign to options
            IAppBuilder branch = app.New();
            configuration(branch);
            options.Branch = (AppFunc)branch.Build(typeof(AppFunc));

            return result;
        }
コード例 #3
0
        public MapPredicateMiddleware(AppFunc next, AppFunc branch, PredicateAsync predicateAsync)
        {
            if (next == null)
            {
                throw new ArgumentNullException("next");
            }
            if (branch == null)
            {
                throw new ArgumentNullException("branch");
            }
            if (predicateAsync == null)
            {
                throw new ArgumentNullException("predicateAsync");
            }

            _next           = next;
            _branch         = branch;
            _predicateAsync = predicateAsync;
        }
コード例 #4
0
        public MapPredicateMiddleware(AppFunc next, AppFunc branch, PredicateAsync predicateAsync)
        {
            if (next == null)
            {
                throw new ArgumentNullException("next");
            }
            if (branch == null)
            {
                throw new ArgumentNullException("branch");
            }
            if (predicateAsync == null)
            {
                throw new ArgumentNullException("predicateAsync");
            }

            _next = next;
            _branch = branch;
            _predicateAsync = predicateAsync;
        }
コード例 #5
0
        public static IAppBuilder MapPredicateAsync(this IAppBuilder builder, PredicateAsync predicate, Action <IAppBuilder> branchConfig)
        {
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }
            if (predicate == null)
            {
                throw new ArgumentNullException("predicate");
            }
            if (branchConfig == null)
            {
                throw new ArgumentNullException("branchConfig");
            }

            IAppBuilder branchBuilder = builder.New();

            branchConfig(branchBuilder);
            return(builder.Use(typeof(MapPredicateMiddleware), branchBuilder.Build(typeof(AppFunc)), predicate));
        }
コード例 #6
0
        public static IAppBuilder MapPredicateAsync <TApp>(this IAppBuilder builder, PredicateAsync predicate, TApp branchApp)
        {
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }
            if (predicate == null)
            {
                throw new ArgumentNullException("predicate");
            }
            if (branchApp == null)
            {
                throw new ArgumentNullException("branchApp");
            }

            IAppBuilder branchBuilder = builder.New();

            branchBuilder.Use(new Func <TApp, TApp>(ignored => branchApp));
            return(builder.Use(typeof(MapPredicateMiddleware), branchBuilder.Build(typeof(AppFunc)), predicate));
        }
コード例 #7
0
        /// <summary>
        /// Branches the request pipeline based on the async result of the given predicate.
        /// </summary>
        /// <param name="app"></param>
        /// <param name="predicate">Invoked asynchronously 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 IApplicationBuilder MapWhenAsync([NotNull] this IApplicationBuilder app, [NotNull] PredicateAsync predicate, [NotNull] Action <IApplicationBuilder> configuration)
        {
            // create branch
            var branchBuilder = app.New();

            configuration(branchBuilder);
            var branch = branchBuilder.Build();

            // put middleware in pipeline
            var options = new MapWhenOptions
            {
                PredicateAsync = predicate,
                Branch         = branch,
            };

            return(app.Use(next => new MapWhenMiddleware(next, options).Invoke));
        }
コード例 #8
0
        /// <remarks>Throws <see cref="ArgumentNullException"/>.</remarks>
        public static async IAsyncEnumerable <T> IfAsync <T>(this ImmutableArray <T> @this, PredicateAsync <T> filter, [EnumeratorCancellation] CancellationToken _ = default)
        {
            filter.AssertNotNull(nameof(filter));

            var count = @this.Length;

            for (var i = 0; i < count; ++i)
            {
                var item = @this[i];
                if (await filter(item))
                {
                    yield return(item);
                }
            }
        }