internal override Task <TResponse> Execute(RequestPipelineContext <TRequest> context) { var filters = context.ServiceProvider.GetServices <IRequestFilter <TRequest, TResponse> >().ToListOptimized(); var handler = context.ServiceProvider.GetRequiredService <IRequestHandler <TRequest, TResponse> >(); return(Execute(filters, ctx => handler.Handle(ctx.Request, ctx.CancellationToken), context)); }
public Task <bool> IsEnabled <TRequest, TResponse>(RequestHandlerToggleInfo toggleInfo, RequestPipelineContext <TRequest> context) where TRequest : IRequest <TResponse> { ToggleInfo = toggleInfo; return(Task.FromResult(Enabled)); }
public Task <TResponse> Execute(RequestPipelineContext <TRequest> context, RequestFilterExecutionDelegate <TResponse> next) { try { Console.WriteLine("unordered filter pre processing"); return(next()); } finally { Console.WriteLine("unordered filter post processing"); } }
internal override async Task<TResponse> Execute(RequestPipelineContext<TRequest> context) { var filters = context.ServiceProvider.GetServices<IRequestFilter<TRequest, TResponse>>().ToListOptimized(); var handlers = context.ServiceProvider.GetServices<IRequestHandler<TRequest, TResponse>>().ToListOptimized(); foreach (var filterSelector in FilterSelectors) { context.CancellationToken.ThrowIfCancellationRequested(); await filterSelector.SelectFilters(filters, context).ConfigureAwait(); } foreach (var handlerSelector in HandlerSelectors) { context.CancellationToken.ThrowIfCancellationRequested(); await handlerSelector.SelectHandlers(handlers, context).ConfigureAwait(); } context.CancellationToken.ThrowIfCancellationRequested(); return await Execute(filters, ctx => HandlerExecutionStrategy.Execute(handlers, ctx), context).ConfigureAwait(); }
public Task <TResponse> Execute <TRequest, TResponse>(List <IRequestHandler <TRequest, TResponse> > handlers, RequestPipelineContext <TRequest> context) where TRequest : IRequest <TResponse> { if (handlers.Count == 0) { throw new InvalidOperationException($"No handlers for request of type '{context.Request.GetType().FullName}'."); } if (handlers.Count > 1) { throw new InvalidOperationException($"Multiple handlers for request of type '{context.Request.GetType().FullName}'."); } return(handlers[0].Handle(context.Request, context.CancellationToken)); }
public Task <Response> Execute(RequestPipelineContext <Request> context, RequestFilterExecutionDelegate <Response> next) { Executed = true; return(next()); }
public async Task <TResponse> Execute <TRequest, TResponse>(List <IRequestHandler <TRequest, TResponse> > handlers, RequestPipelineContext <TRequest> context) where TRequest : IRequest <TResponse> { if (handlers.Count <= 1) { return(await DefaultRequestHandlerExecutionStrategy.Instance.Execute(handlers, context).ConfigureAwait()); } using var cts = CancellationTokenSource.CreateLinkedTokenSource(context.CancellationToken); var tasks = handlers.Select(x => x.Handle(context.Request, cts.Token)).ToList(); List <Exception> exceptions = null; while (tasks.Count != 0) { context.CancellationToken.ThrowIfCancellationRequested(); var task = await Task.WhenAny(tasks).ConfigureAwait(); if (task.Status != TaskStatus.RanToCompletion) { if (exceptions == null) { exceptions = new List <Exception>(); } exceptions.Add(task.Exception); tasks.Remove(task); continue; } if (exceptions != null) { var exceptionHandler = context.ServiceProvider.GetService <IBackgroundExceptionHandler>(); if (exceptionHandler != null) { await exceptionHandler.Handle(exceptions, context.CancellationToken).ConfigureAwait(); } } cts.Cancel(); return(task.Result); } throw new AggregateException(exceptions); }
public async Task SelectHandlers <TRequest, TResponse>(List <IRequestHandler <TRequest, TResponse> > handlers, RequestPipelineContext <TRequest> context) where TRequest : IRequest <TResponse> { var toggle = context.ServiceProvider.GetRequiredService <IRequestHandlerToggle>(); var enabled = await toggle.IsEnabled <TRequest, TResponse>(_toggleInfo, context).ConfigureAwait(); if (!enabled) { handlers.RemoveAll(x => x.GetType() == _toggleInfo.ToggleEnabledHandlerType); } else if (_toggleInfo.ToggleDisabledHandlerType != null) { handlers.RemoveAll(x => x.GetType() == _toggleInfo.ToggleDisabledHandlerType); } }
public Task <TResponse> Execute(RequestPipelineContext <TRequest> context, RequestFilterExecutionDelegate <TResponse> next) { Action.Invoke("filter"); return(next()); }
public Task <TResponse> Execute <TRequest, TResponse>(List <IRequestHandler <TRequest, TResponse> > handlers, RequestPipelineContext <TRequest> context) where TRequest : IRequest <TResponse> { Action.Invoke("execution strategy"); return(handlers.Single().Handle(context.Request, context.CancellationToken)); }
public Task <string> Execute(RequestPipelineContext <Request> context, RequestFilterExecutionDelegate <string> next) { return(context.Request.Terminate ? Task.FromResult("terminated") : next()); }
public Task <bool> IsEnabled <TRequest, TResponse>(RequestFilterToggleInfo toggleInfo, RequestPipelineContext <TRequest> context) where TRequest : IRequest <TResponse> { Console.WriteLine($"evaluating {toggleInfo.ToggleName}"); return(Task.FromResult(Enabled)); }
public async Task <string> Execute(RequestPipelineContext <Echo> context, RequestFilterExecutionDelegate <string> next) { Console.WriteLine("echo filter"); return(await next()); }
public Task <string> Execute(RequestPipelineContext <Request> context, RequestFilterExecutionDelegate <string> next) { Console.WriteLine("filter 3"); return(next()); }
public Task <string> Execute(RequestPipelineContext <Request> context, RequestFilterExecutionDelegate <string> next) { context.Request.Text += Text; return(next()); }
public Task <bool> IsEnabled <TRequest, TResponse>(RequestHandlerExperimentInfo experimentInfo, RequestPipelineContext <TRequest> context) where TRequest : IRequest <TResponse> { return(Task.FromResult(_counter++ != 0)); }
public async Task <string> Execute(RequestPipelineContext <Echo> context, RequestFilterExecutionDelegate <string> next) { context.Request.Message = $"pre processing: {context.Request.Message}"; return($"post processing: {await next()}"); }
public Task <TResponse> Execute(RequestPipelineContext <TRequest> context, RequestFilterExecutionDelegate <TResponse> next) { Console.WriteLine("generic filter"); return(next()); }
public Task SelectHandlers <TRequest, TResponse>(List <IRequestHandler <TRequest, TResponse> > handlers, RequestPipelineContext <TRequest> context) where TRequest : IRequest <TResponse> { handlers.Add(new RequestHandler <TRequest, TResponse> { Action = context.ServiceProvider.GetRequiredService <Action <string> >() }); return(Task.CompletedTask); }
public async Task <TResponse> Execute <TRequest, TResponse>(List <IRequestHandler <TRequest, TResponse> > handlers, RequestPipelineContext <TRequest> context) where TRequest : IRequest <TResponse> { if (handlers.Count <= 1) { return(await DefaultRequestHandlerExecutionStrategy.Instance.Execute(handlers, context).ConfigureAwait()); } var baselineHandler = GetBaselineHandler(handlers); var toggle = context.ServiceProvider.GetRequiredService <IRequestHandlerExperimentToggle>(); var request = context.Request; var cancellationToken = context.CancellationToken; if (!await toggle.IsEnabled <TRequest, TResponse>(_experimentInfo, context).ConfigureAwait()) { return(await baselineHandler.Handle(request, cancellationToken).ConfigureAwait()); } var observer = context.ServiceProvider.GetRequiredService <IRequestHandlerExperimentObserver>(); var tasks = handlers.Select(handler => Execute(handler, request, cancellationToken)).ToListOptimized(); var executions = await Task.WhenAll(tasks).ConfigureAwait(); var baselineExecution = executions.Single(x => x.Handler == baselineHandler); var experimentExecutions = executions.Where(x => x != baselineExecution).ToList(); var experiment = new RequestHandlerExperiment <TRequest, TResponse> { BaselineExecution = baselineExecution, CancellationToken = cancellationToken, ExperimentalExecutions = experimentExecutions, Request = request }; await observer.Observe(experiment).ConfigureAwait(); return(await baselineExecution.Task.ConfigureAwait()); }
public Task <Void> Execute(RequestPipelineContext <Request> context, RequestFilterExecutionDelegate <Void> next) { _cancellationTokenSource.Cancel(); Executed = true; return(next()); }