示例#1
0
        public async void UseExceptionInterceptManager_OptionRethrowExceptionTrue_FakeMiddlewareWillReceiveBubbledException()
        {
            var receivedException = (Exception)null;
            var thrownException   = new InvalidOperationException("SomeException");
            var exceptionBuddled  = false;

            var builder = new WebHostBuilder()
                          .ConfigureServices(services =>
            {
                // arrange
                services.AddExceptionInterceptManager();
            })
                          .Configure(app =>
            {
                // inject a fake middleware in the pipeline.
                app.Use(next => async context =>
                {
                    try
                    {
                        await next.Invoke(context);
                    }
                    catch (Exception ex)
                    {
                        receivedException = ex;     // this exception should be received.
                        exceptionBuddled  = true;
                    }
                });

                // arrange
                var option = new ExceptionInterceptOptions()
                {
                    RethrowException = true
                };                                                                            // bubble exception
                app.UseExceptionInterceptManager(option);

                // act
                app.Run(httpContext =>
                {
                    throw thrownException;
                });
            });

            using (var server = new TestServer(builder))
            {
                using (var client = server.CreateClient())
                {
                    await client.GetAsync("http://someurl.com");
                }
            }

            // assert
            Assert.Same(thrownException, receivedException);
            Assert.True(exceptionBuddled, "The exception should have been bubbled.");
        }
        /// <summary>
        /// Uses the exception intercept manager.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <returns></returns>
        public static IApplicationBuilder UseExceptionInterceptManager(this IApplicationBuilder builder, ExceptionInterceptOptions options = null)
        {
            var middlewareOptions = options ?? new ExceptionInterceptOptions()
            {
                RethrowException = false
            };

            return(builder.UseMiddleware <ExceptionInterceptMiddleware>(middlewareOptions));
        }