public async Task RunAsync_WithStrongTypedMiddleware_ShouldBeExecuted(IPipelineBuilder pipeline) { var data = new Data(); await pipeline.ForAsync <Data>().UseAsync <AsyncMiddlewareA>().UseAsync <AsyncMiddlewareB>().UseAsync <AsyncMiddlewareC>().RunAsync(data); data.Steps[0].ShouldBe(AsyncMiddlewareA.Start); data.Steps[1].ShouldBe(AsyncMiddlewareB.Start); data.Steps[2].ShouldBe(AsyncMiddlewareC.Start); data.Steps[3].ShouldBe(AsyncMiddlewareC.End); data.Steps[4].ShouldBe(AsyncMiddlewareB.End); data.Steps[5].ShouldBe(AsyncMiddlewareA.End); }
public async Task RunAsync_WithCancellationToken_ShouldBeThrowException(IPipelineBuilder pipeline) { var data = new Data(); using (var ds = new CancellationTokenSource(TimeSpan.FromSeconds(1))) { var success = false; try { await pipeline.ForAsync <Data>().UseAsync <AsyncMiddlewareD>().UseAsync <AsyncMiddlewareE>().RunAsync(data, ds.Token); } catch (Exception ex) { success = true; } success.ShouldBeTrue(); } }
public async Task RunAsync_WithAnonymousMiddleware_ShouldBeExecuted(IPipelineBuilder pipeline) { var data = new Data(); await pipeline.ForAsync <Data>() .UseAsync(async(c, next) => { c.Data.Steps.Add("Start Anonymous A"); await next(c); c.Data.Steps.Add("End Anonymous A"); }) .UseAsync(async(c, next) => { c.Data.Steps.Add("Start Anonymous B"); await next(c); c.Data.Steps.Add("End Anonymous B"); }) .UseAsync((c, next) => { c.Data.Steps.Add("Start Anonymous C"); c.Data.Steps.Add("End Anonymous C"); return(Task.CompletedTask); }) .RunAsync(data); data.Steps[0].ShouldBe("Start Anonymous A"); data.Steps[1].ShouldBe("Start Anonymous B"); data.Steps[2].ShouldBe("Start Anonymous C"); data.Steps[3].ShouldBe("End Anonymous C"); data.Steps[4].ShouldBe("End Anonymous B"); data.Steps[5].ShouldBe("End Anonymous A"); }
public async Task <HttpResponse> SendAsync(HttpRequest request) { try { var context = new HttpContext(request); var chain = _pipelinebuilder.ForAsync <HttpContext>(); foreach (var type in request.MiddlewareTypes) { chain.UseAsync(type); } UpdateRequestUri(request); await chain.UseAsync <HttpMiddelware>().RunAsync(context, request.CancellationToken); return(context.Response); } catch (Exception ex) { return(new HttpResponse(request, null, ex, 0)); } }