public async Task EndMiddleware_DoesNotThrow_IfExceptionHandled() { // Arrange var services = new ServiceCollection(); var appBuilder = new ApplicationBuilder(services.BuildServiceProvider()); var pipelineBuilderService = new MiddlewareFilterBuilder(new MiddlewareFilterConfigurationProvider()) { ApplicationBuilder = appBuilder, }; Pipeline1.ConfigurePipeline = ab => { ab.Use((ctx, next) => { return(next(ctx)); }); }; var middlewareFilterFeature = new MiddlewareFilterFeature { ResourceExecutionDelegate = () => { var actionContext = new ActionContext( new DefaultHttpContext(), new RouteData(), new ActionDescriptor(), new ModelStateDictionary()); var context = new ResourceExecutedContext(actionContext, new List <IFilterMetadata>()) { Exception = new InvalidOperationException("Error!!!"), ExceptionHandled = true, }; return(Task.FromResult(context)); }, }; var features = new FeatureCollection(); features.Set <IMiddlewareFilterFeature>(middlewareFilterFeature); var httpContext = new DefaultHttpContext(features); // Act var pipeline = pipelineBuilderService.GetPipeline(typeof(Pipeline1)); // Assert Assert.NotNull(pipeline); // Does not throw. await pipeline(httpContext); }
public Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next) { var httpContext = context.HttpContext; // Capture the current context into the feature. This will later be used in the end middleware to continue // the execution flow to later MVC layers. // Example: // this filter -> user-middleware1 -> user-middleware2 -> the-end-middleware -> resource filters or model binding var feature = new MiddlewareFilterFeature() { ResourceExecutionDelegate = next, ResourceExecutingContext = context }; httpContext.Features.Set <IMiddlewareFilterFeature>(feature); return(_middlewarePipeline(httpContext)); }
public async Task EndMiddleware_PropagatesFullExceptionInfo_ToEarlierMiddleware() { // Arrange var services = new ServiceCollection(); var appBuilder = new ApplicationBuilder(services.BuildServiceProvider()); var pipelineBuilderService = new MiddlewareFilterBuilder(new MiddlewareFilterConfigurationProvider()) { ApplicationBuilder = appBuilder, }; Pipeline1.ConfigurePipeline = ab => { ab.Use((ctx, next) => { return(next(ctx)); }); }; var middlewareFilterFeature = new MiddlewareFilterFeature { ResourceExecutionDelegate = () => { ExceptionDispatchInfo exceptionInfo; try { // Create a small stack trace. throw new InvalidOperationException("Error!!!"); } catch (Exception ex) { exceptionInfo = ExceptionDispatchInfo.Capture(ex); } var actionContext = new ActionContext( new DefaultHttpContext(), new RouteData(), new ActionDescriptor(), new ModelStateDictionary()); var context = new ResourceExecutedContext(actionContext, new List <IFilterMetadata>()) { ExceptionDispatchInfo = exceptionInfo, }; return(Task.FromResult(context)); }, }; var features = new FeatureCollection(); features.Set <IMiddlewareFilterFeature>(middlewareFilterFeature); var httpContext = new DefaultHttpContext(features); // Act var pipeline = pipelineBuilderService.GetPipeline(typeof(Pipeline1)); // Assert Assert.NotNull(pipeline); var exception = await Assert.ThrowsAsync <InvalidOperationException>(() => pipeline(httpContext)); Assert.Null(exception.InnerException); Assert.Equal("Error!!!", exception.Message); var stack = exception.StackTrace; Assert.Contains(typeof(MiddlewareFilterBuilder).FullName, stack); Assert.Contains(typeof(MiddlewareFilterBuilderTest).FullName, stack); Assert.Contains(nameof(EndMiddleware_PropagatesFullExceptionInfo_ToEarlierMiddleware), stack); }