Beispiel #1
0
        public async void WrapperResolvesActionFilterFromDependencyScope()
        {
            var builder = new ContainerBuilder();

            builder.Register <ILogger>(c => new Logger()).InstancePerDependency();
            var configuration = new HttpConfiguration();

            builder.RegisterWebApiFilterProvider(configuration);
            var activationCount = 0;

            builder.Register <IAutofacActionFilter>(c => new TestActionFilter(c.Resolve <ILogger>()))
            .AsWebApiActionFilterFor <TestController>(c => c.Get())
            .InstancePerRequest()
            .OnActivated(e => activationCount++)
            .GetMetadata(out var filterMetadata);

            var container = builder.Build();

            var resolver          = new AutofacWebApiDependencyResolver(container);
            var controllerContext = CreateControllerContext(resolver);
            var methodInfo        = typeof(TestController).GetMethod("Get");
            var actionDescriptor  = CreateActionDescriptor(methodInfo);
            var actionContext     = new HttpActionContext(controllerContext, actionDescriptor);

            var wrapper = new ContinuationActionFilterWrapper(filterMetadata.ToSingleFilterHashSet());

            await wrapper.ExecuteActionFilterAsync(actionContext, CancellationToken.None, () => Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)));

            Assert.Equal(1, activationCount);
        }
Beispiel #2
0
        public void TransactionScopePreservedBetweenStandardFilters()
        {
            // Issue #34 - Async/await context lost between filters.
            var builder = new ContainerBuilder();

            TransactionScope scope = null;

            // Since Autofac 4.0.1 filters will resolve in registration order.
            builder.Register(s => new TestCallbackActionFilter(
                                 () =>
                                 scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled),
                                 () =>
            {
                Assert.NotNull(Transaction.Current);
                scope.Dispose();
                Assert.Null(Transaction.Current);
            }))
            .AsWebApiActionFilterFor <TestController>(c => c.Get())
            .InstancePerRequest()
            .GetMetadata(out var testFilter1Meta);

            builder.Register(s => new TestCallbackActionFilter(
                                 () =>
                                 Assert.NotNull(Transaction.Current),
                                 () =>
                                 Assert.NotNull(Transaction.Current)))
            .AsWebApiActionFilterFor <TestController>(c => c.Get())
            .InstancePerRequest()
            .GetMetadata(out var testFilter2Meta);

            var configuration = new HttpConfiguration();

            builder.RegisterWebApiFilterProvider(configuration);

            var container = builder.Build();

            var resolver          = new AutofacWebApiDependencyResolver(container);
            var controllerContext = CreateControllerContext(resolver);
            var methodInfo        = typeof(TestController).GetMethod("Get");
            var actionDescriptor  = CreateActionDescriptor(methodInfo);
            var actionContext     = new HttpActionContext(controllerContext, actionDescriptor);
            var wrapper           = new ContinuationActionFilterWrapper(new HashSet <FilterMetadata>
            {
                testFilter1Meta,
                testFilter2Meta
            });

            wrapper.ExecuteActionFilterAsync(
                actionContext,
                CancellationToken.None,
                () =>
            {
                Assert.NotNull(Transaction.Current);
                return(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)));
            }).Wait();
        }
Beispiel #3
0
        public async void RunsFiltersInCorrectOrder()
        {
            // Issue #16: Filters need to run 1, 2, 3 in Executing but 3, 2, 1 in Executed.
            var builder = new ContainerBuilder();
            var order   = new List <string>();

            builder.Register(ctx => new DelegatingLogger(s => order.Add(s)))
            .As <ILogger>()
            .SingleInstance();
            builder.RegisterType <TestActionFilter>()
            .AsWebApiActionFilterFor <TestController>(c => c.Get())
            .InstancePerRequest()
            .GetMetadata(out var testFilter1Meta);
            builder.RegisterType <TestActionFilter2>()
            .AsWebApiActionFilterFor <TestController>(c => c.Get())
            .InstancePerRequest()
            .GetMetadata(out var testFilter2Meta);

            var configuration = new HttpConfiguration();

            builder.RegisterWebApiFilterProvider(configuration);

            var container = builder.Build();

            var resolver          = new AutofacWebApiDependencyResolver(container);
            var controllerContext = CreateControllerContext(resolver);
            var methodInfo        = typeof(TestController).GetMethod("Get");
            var actionDescriptor  = CreateActionDescriptor(methodInfo);
            var actionContext     = new HttpActionContext(controllerContext, actionDescriptor);
            var wrapper           = new ContinuationActionFilterWrapper(new HashSet <FilterMetadata>
            {
                testFilter1Meta,
                testFilter2Meta
            });

            await wrapper.ExecuteActionFilterAsync(
                actionContext,
                CancellationToken.None,
                () => Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)));

            Assert.Equal("TestActionFilter.OnActionExecutingAsync", order[0]);
            Assert.Equal("TestActionFilter2.OnActionExecutingAsync", order[1]);
            Assert.Equal("TestActionFilter2.OnActionExecutedAsync", order[2]);
            Assert.Equal("TestActionFilter.OnActionExecutedAsync", order[3]);
        }
Beispiel #4
0
        public async void StopsIfFilterOnExecutingSetsResponse()
        {
            // Issue #30.
            // The filter behaviour if a response is set should be as follows, to
            // mirror the functionality of filters in the normal IActionFilter implementations.
            //
            // If a filter sets the response:
            // - OnActionExecutingAsync from subsequent calls should not be invoked.
            // - Its own OnActionExecutedAsync should not be invoked.
            // - OnActionExecutedAsync for prior filters should still be invoked.
            var builder = new ContainerBuilder();
            var order   = new List <string>();

            builder.Register(ctx => new DelegatingLogger(s => order.Add(s)))
            .As <ILogger>()
            .SingleInstance();
            builder.RegisterType <TestActionFilter>()
            .AsWebApiActionFilterFor <TestController>(c => c.Get())
            .InstancePerRequest()
            .GetMetadata(out var testActionFilterMetadata);
            builder.RegisterType <TestActionFilter2>()
            .AsWebApiActionFilterFor <TestController>(c => c.Get())
            .InstancePerRequest()
            .GetMetadata(out var testActionFilter2Metadata);
            builder.RegisterType <TestActionFilterWithResponse>()
            .AsWebApiActionFilterFor <TestController>(c => c.Get())
            .InstancePerRequest()
            .GetMetadata(out var testActionFilterWithResponseMetadata);
            builder.RegisterType <TestActionFilter3>()
            .AsWebApiActionFilterFor <TestController>(c => c.Get())
            .InstancePerRequest()
            .GetMetadata(out var testActionFilter3Metadata);

            var configuration = new HttpConfiguration();

            builder.RegisterWebApiFilterProvider(configuration);

            var container = builder.Build();

            var resolver          = new AutofacWebApiDependencyResolver(container);
            var controllerContext = CreateControllerContext(resolver);
            var methodInfo        = typeof(TestController).GetMethod("Get");
            var actionDescriptor  = CreateActionDescriptor(methodInfo);
            var actionContext     = new HttpActionContext(controllerContext, actionDescriptor);

            var wrapper = new ContinuationActionFilterWrapper(new HashSet <FilterMetadata>
            {
                testActionFilterMetadata,
                testActionFilterWithResponseMetadata,
                testActionFilter2Metadata,
                testActionFilter3Metadata
            });

            await wrapper.ExecuteActionFilterAsync(
                actionContext,
                CancellationToken.None,
                () => throw new Exception("Should never reach here because a filter set the response."));

            Assert.Equal("TestActionFilter.OnActionExecutingAsync", order[0]);
            Assert.Equal("TestActionFilter2.OnActionExecutingAsync", order[1]);
            Assert.Equal("TestActionFilterWithResponse.OnActionExecutingAsync", order[2]);
            Assert.Equal("TestActionFilter2.OnActionExecutedAsync", order[3]);
            Assert.Equal("TestActionFilter.OnActionExecutedAsync", order[4]);
            Assert.Equal(5, order.Count);
        }