public void CanAddMiddlewareInReversePhaseOrder()
        {
            var pipelineBuilder = new ResolvePipelineBuilder(PipelineType.Service);

            var order = new List <string>();

            pipelineBuilder.Use("3", PipelinePhase.Sharing, (ctxt, next) =>
            {
                order.Add("3");
                next(ctxt);
            });
            pipelineBuilder.Use("2", PipelinePhase.ScopeSelection, (ctxt, next) =>
            {
                order.Add("2");
                next(ctxt);
            });
            pipelineBuilder.Use("1", PipelinePhase.ResolveRequestStart, (ctxt, next) =>
            {
                order.Add("1");
                next(ctxt);
            });

            var built = pipelineBuilder.Build();

            built.Invoke(new PipelineRequestContextStub());

            Assert.Collection(
                order,
                el => Assert.Equal("1", el.ToString()),
                el => Assert.Equal("2", el.ToString()),
                el => Assert.Equal("3", el.ToString()));
        }
Beispiel #2
0
        /// <summary>
        /// Get an invoker for an activator's pipeline.
        /// </summary>
        /// <param name="activator">The activator.</param>
        /// <param name="registry">The applicable component registry.</param>
        /// <returns>A func to call that invokes the pipeline.</returns>
        public static Func <ILifetimeScope, IEnumerable <Parameter>, T> GetPipelineInvoker <T>(this IInstanceActivator activator, IComponentRegistry registry)
        {
            var services        = new RegistryServices(registry);
            var pipelineBuilder = new ResolvePipelineBuilder(PipelineType.Registration);

            activator.ConfigurePipeline(services, pipelineBuilder);

            var built = pipelineBuilder.Build();

            return((scope, parameters) =>
            {
                // To get the sharing scope from what might be a container, we're going to resolve the lifetime scope.
                var lifetimeScope = scope.Resolve <ILifetimeScope>() as LifetimeScope;

                var request = new DefaultResolveRequestContext(
                    new ResolveOperation(lifetimeScope, lifetimeScope.DiagnosticSource),
                    new ResolveRequest(new TypedService(typeof(T)), Mocks.GetResolvableImplementation(), parameters),
                    lifetimeScope,
                    lifetimeScope.DiagnosticSource);

                built.Invoke(request);

                return (T)request.Instance;
            });
        }
        public void CannotAddRegistrationMiddlewareToServicePipeline()
        {
            var pipelineBuilder = new ResolvePipelineBuilder(PipelineType.Service);
            var order           = new List <string>();

            Assert.Throws <InvalidOperationException>(() => pipelineBuilder.Use(PipelinePhase.RegistrationPipelineStart, (ctxt, next) => { }));
        }
        public void CanControlPhaseAddPriorityWithPrecedingPhase()
        {
            var pipelineBuilder = new ResolvePipelineBuilder(PipelineType.Service);

            var order = new List <string>();

            pipelineBuilder.Use("3", PipelinePhase.ScopeSelection, (ctxt, next) =>
            {
                order.Add("3");
                next(ctxt);
            });
            pipelineBuilder.Use("1", PipelinePhase.ResolveRequestStart, (ctxt, next) =>
            {
                order.Add("1");
                next(ctxt);
            });
            pipelineBuilder.Use("2", PipelinePhase.ScopeSelection, MiddlewareInsertionMode.StartOfPhase, (ctxt, next) =>
            {
                order.Add("2");
                next(ctxt);
            });

            var built = pipelineBuilder.Build();

            built.Invoke(new PipelineRequestContextStub());

            Assert.Collection(
                order,
                el => Assert.Equal("1", el.ToString()),
                el => Assert.Equal("2", el.ToString()),
                el => Assert.Equal("3", el.ToString()));
        }
        public void CanAddMultipleMiddlewareToPipelineWithExistingMiddleware()
        {
            var pipelineBuilder = new ResolvePipelineBuilder(PipelineType.Service);
            var order           = new List <string>();

            pipelineBuilder.UseRange(new[]
            {
                new DelegateMiddleware("1", PipelinePhase.ResolveRequestStart, (ctxt, next) =>
                {
                    order.Add("1");
                    next(ctxt);
                }),
                new DelegateMiddleware("3", PipelinePhase.ScopeSelection, (ctxt, next) =>
                {
                    order.Add("3");
                    next(ctxt);
                }),
                new DelegateMiddleware("5", PipelinePhase.ServicePipelineEnd, (ctxt, next) =>
                {
                    order.Add("5");
                    next(ctxt);
                })
            });

            pipelineBuilder.UseRange(new[]
            {
                new DelegateMiddleware("2", PipelinePhase.ResolveRequestStart, (ctxt, next) =>
                {
                    order.Add("2");
                    next(ctxt);
                }),
                new DelegateMiddleware("4", PipelinePhase.ScopeSelection, (ctxt, next) =>
                {
                    order.Add("4");
                    next(ctxt);
                }),
                new DelegateMiddleware("6", PipelinePhase.ServicePipelineEnd, (ctxt, next) =>
                {
                    order.Add("6");
                    next(ctxt);
                })
            });

            var built = pipelineBuilder.Build();

            built.Invoke(new PipelineRequestContextStub());

            Assert.Collection(
                order,
                el => Assert.Equal("1", el.ToString()),
                el => Assert.Equal("2", el.ToString()),
                el => Assert.Equal("3", el.ToString()),
                el => Assert.Equal("4", el.ToString()),
                el => Assert.Equal("5", el.ToString()),
                el => Assert.Equal("6", el.ToString()));
        }
        public void CannotAddBadPhaseToPipelineInUseRange()
        {
            var pipelineBuilder = new ResolvePipelineBuilder(PipelineType.Service);
            var order           = new List <string>();

            Assert.Throws <InvalidOperationException>(() => pipelineBuilder.UseRange(new[]
            {
                new DelegateMiddleware("1", PipelinePhase.ResolveRequestStart, (ctxt, next) => { }),
                new DelegateMiddleware("4", PipelinePhase.Activation, (ctxt, next) => { }),
                new DelegateMiddleware("2", PipelinePhase.ScopeSelection, (ctxt, next) => { }),
            }));
        }
        public void AddMultipleMiddlewareOutOfOrderThrows()
        {
            var pipelineBuilder = new ResolvePipelineBuilder(PipelineType.Service);
            var order           = new List <string>();

            pipelineBuilder.UseRange(new[]
            {
                new DelegateMiddleware("1", PipelinePhase.ResolveRequestStart, (ctxt, next) =>
                {
                    order.Add("1");
                    next(ctxt);
                }),
                new DelegateMiddleware("2", PipelinePhase.ScopeSelection, (ctxt, next) =>
                {
                    order.Add("2");
                    next(ctxt);
                }),
                new DelegateMiddleware("4", PipelinePhase.ServicePipelineEnd, (ctxt, next) =>
                {
                    order.Add("3");
                    next(ctxt);
                })
            });

            Assert.Throws <InvalidOperationException>(() => pipelineBuilder.UseRange(new[]
            {
                new DelegateMiddleware("1", PipelinePhase.ResolveRequestStart, (ctxt, next) =>
                {
                    order.Add("1");
                    next(ctxt);
                }),
                new DelegateMiddleware("4", PipelinePhase.ServicePipelineEnd, (ctxt, next) =>
                {
                    order.Add("4");
                    next(ctxt);
                }),
                new DelegateMiddleware("2", PipelinePhase.ScopeSelection, (ctxt, next) =>
                {
                    order.Add("2");
                    next(ctxt);
                }),
            }));
        }
        public void CanHaveSingleStage()
        {
            var pipelineBuilder = new ResolvePipelineBuilder(PipelineType.Service);
            var order           = new List <string>();

            pipelineBuilder.Use(PipelinePhase.ResolveRequestStart, (ctxt, next) =>
            {
                order.Add("1");
                next(ctxt);
            });

            var built = pipelineBuilder.Build();

            built.Invoke(new PipelineRequestContextStub());

            Assert.Collection(
                order,
                e => Assert.Equal("1", e));
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="RegistrationBuilder{TLimit, TActivatorData, TRegistrationStyle}"/> class.
        /// </summary>
        /// <param name="defaultService">The default service.</param>
        /// <param name="activatorData">The activator data.</param>
        /// <param name="style">The registration style.</param>
        public RegistrationBuilder(Service defaultService, TActivatorData activatorData, TRegistrationStyle style)
        {
            if (defaultService == null)
            {
                throw new ArgumentNullException(nameof(defaultService));
            }

            if (activatorData == null)
            {
                throw new ArgumentNullException(nameof(activatorData));
            }

            if (style == null)
            {
                throw new ArgumentNullException(nameof(style));
            }

            ActivatorData     = activatorData;
            RegistrationStyle = style;
            RegistrationData  = new RegistrationData(defaultService);
            ResolvePipeline   = new ResolvePipelineBuilder(PipelineType.Registration);
        }