public static void UseTransformations <TContext, TBaseContextType>( this IMiddlewareAppBuilder <TContext, TBaseContextType> subject, Action <ITransformationMiddlewareBuilder <TContext> > setup) where TContext : IContext { var builder = new TransformationMiddlewareBuilder <TContext>(); setup(builder); subject.Use( async(ctx, next) => { var logger = ctx.Services.GetService <ILogger <TransformationMiddlewareBuilder <TContext> > >(); var transforms = builder.Handlers .Select(async x => await x(ctx)) .ToArray(); await Task.WhenAll(transforms); var transformedContexts = transforms .Where(x => !x.IsFaulted && !x.IsCanceled && x.IsCompleted) .Select(x => x.Result) .ToArray(); var failedTranformations = transforms .Where(x => x.IsFaulted || x.IsCanceled) .Count(); if (failedTranformations != 0) { logger.LogWarning("Failed to apply {failed} of {allTranformations}", failedTranformations, transformedContexts.Count()); } if (!transformedContexts.Any(x => x.transformApplies) || transformedContexts.Any(x => x.considerOrigin)) { await next(ctx); } var transformedContextHandlers = transformedContexts .Where(x => x.transformApplies) .Select(async x => await next(x.transformed)) .ToArray(); await Task.WhenAll(transformedContextHandlers); }); }
public static IMiddlewareAppBuilder <TContext, TBaseContextType> UseProjections <TContext, TBaseContextType>(this IMiddlewareAppBuilder <TContext, TBaseContextType> app, Action <IProjectionHostBuilder <TContext> > factory) where TContext : IContext { var projBuilder = new ProjectionHostBuilder <TContext>(); Func <TContext, Func <Task>, Task> middleware = (ctx, next) => { throw new ArgumentNullException("There is no handler for the projection middleware. Did you forget to build the AppBuilder?"); }; factory(projBuilder); app.Use((ctx, next) => middleware(ctx, next)); app.Configure(() => middleware = projBuilder.Build()); return(app); }