public virtual async Task Invoke(HttpContext httpContext) { var deepestDepth = 0; IPipeline pipeline = null; if (_pipelineCollectionAccessor.Pipelines?.Any() == true) { foreach (var p in _pipelineCollectionAccessor.Pipelines) { if (await p.ResolveAsync(httpContext)) { var depth = _decisionTree.GetPipelinePath(p).Count; if (depth > deepestDepth) { pipeline = p; deepestDepth = depth; } } } } if (pipeline != null) { httpContext.Items[_options.Value.PipelineHttpContextItemKey] = pipeline; _logger.LogInformation(InformationEventId, $"Resolved pipeline: {pipeline.Name}"); var requestDelegate = _pipelines.GetOrAdd(pipeline, new Lazy <RequestDelegate>(() => { var builder = _app.New(); Task.WaitAll(pipeline.ConfigurePipeline(builder)); builder.Run(_next); return(builder.Build()); })); await requestDelegate.Value(httpContext); } else { if (_defaultRequestDelegate == null && _defaultPipelineConfiguration != null) { var builder = _app.New(); _defaultPipelineConfiguration(builder); builder.Run(_next); _defaultRequestDelegate = builder.Build(); } await(_defaultRequestDelegate ?? _next)(httpContext); } }