示例#1
0
        /// <inheritdoc/>
        public void ProvideMiddleware(Service service, IComponentRegistryServices availableServices, IResolvePipelineBuilder pipelineBuilder)
        {
            if (service is IServiceWithType swt)
            {
                var closedDecoratorService = new DecoratorService(swt.ServiceType, _decoratorService.Condition);

                // Try to bind to the service.
                if (OpenGenericServiceBinder.TryBindOpenGenericTypedService(closedDecoratorService, _registrationData.Services, _activatorData.ImplementationType, out var constructedImplementationType, out var services))
                {
                    // Create a new closed-generic registration.
                    var registration = new ComponentRegistration(
                        Guid.NewGuid(),
                        new ReflectionActivator(constructedImplementationType, _activatorData.ConstructorFinder, _activatorData.ConstructorSelector, _activatorData.ConfiguredParameters, _activatorData.ConfiguredProperties),
                        _registrationData.Lifetime,
                        _registrationData.Sharing,
                        _registrationData.Ownership,
                        services,
                        _registrationData.Metadata);

                    // Build the resolve pipeline so we can invoke it.
                    registration.BuildResolvePipeline(availableServices);

                    // Add our closed decorator middleware to the pipeline.
                    pipelineBuilder.Use(new DecoratorMiddleware(closedDecoratorService, registration), MiddlewareInsertionMode.StartOfPhase);
                }
            }
        }
 /// <inheritdoc/>
 public void ProvideMiddleware(Service service, IComponentRegistryServices availableServices, IResolvePipelineBuilder pipelineBuilder)
 {
     if (service is IServiceWithType swt && swt.ServiceType == _serviceType)
     {
         // This is the right type, add the middleware.
         pipelineBuilder.Use(_middleware, _insertionMode);
     }
 }
示例#3
0
        /// <inheritdoc/>
        public void ConfigurePipeline(IComponentRegistryServices componentRegistryServices, IResolvePipelineBuilder pipelineBuilder)
        {
            if (pipelineBuilder is null)
            {
                throw new ArgumentNullException(nameof(pipelineBuilder));
            }

            pipelineBuilder.Use(this.DisplayName(), PipelinePhase.Activation, MiddlewareInsertionMode.EndOfPhase, (ctxt, next) =>
            {
                ctxt.Instance = ActivateInstance(ctxt, ctxt.Parameters);

                next(ctxt);
            });
        }
示例#4
0
        /// <inheritdoc/>
        public void ConfigurePipeline(IComponentRegistryServices componentRegistryServices, IResolvePipelineBuilder pipelineBuilder)
        {
            pipelineBuilder.Use(PipelinePhase.Activation, (context, next) =>
            {
                // Get a reference to the actual lifetime scope.
                var scope = context.Resolve <ILifetimeScope>();

                var poolPolicy = new AutofacPooledObjectPolicy <TLimit>(_pooledInstanceService, scope, _policy);

                // The pool provider will create a disposable pool if the TLimit implements IDisposable.
                var pool = _poolProvider.Create(poolPolicy);

                context.Instance = pool;
            });
        }
示例#5
0
        /// <inheritdoc/>
        public void ConfigurePipeline(IComponentRegistryServices componentRegistryServices, IResolvePipelineBuilder pipelineBuilder)
        {
            pipelineBuilder.Use(PipelinePhase.Activation, (ctxt, next) =>
            {
                var pool           = (ObjectPool <TLimit>)ctxt.ResolveService(_poolService);
                var didGetFromPool = false;

                TLimit PoolGet()
                {
                    didGetFromPool = true;
                    return(pool.Get());
                }

                var poolItem = _registrationPolicy.Get(ctxt, ctxt.Parameters, PoolGet);

                if (poolItem is null)
                {
                    throw new InvalidOperationException(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            PoolGetActivatorResources.PolicyMustReturnInstance,
                            _registrationPolicy.GetType().FullName,
                            typeof(TLimit).FullName));
                }

                if (didGetFromPool)
                {
                    if (poolItem is IPooledComponent poolAwareComponent)
                    {
                        poolAwareComponent.OnGetFromPool(ctxt, ctxt.Parameters);
                    }

                    // Need to return a 'container' that
                    // gets unpacked just after we're done sharing.
                    // That way disposal of the scope will return to the pool.
                    ctxt.Instance = new PooledInstanceTracker <TLimit>(pool, poolItem);
                }
                else
                {
                    // Instance did not come from the pool, so just use it directly.
                    ctxt.Instance = poolItem;
                }
            });
        }
示例#6
0
        public void ProvideMiddleware(Service service, IComponentRegistryServices availableServices, IResolvePipelineBuilder pipelineBuilder)
        {
            pipelineBuilder.Use(PipelinePhase.ServicePipelineEnd, (context, next) =>
            {
                next(context);

                object?instance = context.Instance;

                if (instance != null)
                {
                    TypeInfo instanceType = instance.GetType().GetTypeInfo();

                    if (TestDependencyManager.CurrentTestDependencyManager.IsGoingToCreateProxyForImplementationType(instanceType))
                    {
                        instance         = _createProxyForService.MakeGenericMethod(instanceType).Invoke(this, new[] { instance }) !;
                        context.Instance = instance;
                    }

                    TestDependencyManager.CurrentTestDependencyManager.Objects.Add(instance);
                }
            });
        }
示例#7
0
        /// <inheritdoc/>
        public void ConfigurePipeline(IComponentRegistryServices componentRegistryServices, IResolvePipelineBuilder pipelineBuilder)
        {
            if (componentRegistryServices is null)
            {
                throw new ArgumentNullException(nameof(componentRegistryServices));
            }

            if (pipelineBuilder is null)
            {
                throw new ArgumentNullException(nameof(pipelineBuilder));
            }

            // Locate the possible constructors at container build time.
            var availableConstructors = ConstructorFinder.FindConstructors(_implementationType);

            if (availableConstructors.Length == 0)
            {
                throw new NoConstructorsFoundException(_implementationType, string.Format(CultureInfo.CurrentCulture, ReflectionActivatorResources.NoConstructorsAvailable, _implementationType, ConstructorFinder));
            }

            var binders = new ConstructorBinder[availableConstructors.Length];

            for (var idx = 0; idx < availableConstructors.Length; idx++)
            {
                binders[idx] = new ConstructorBinder(availableConstructors[idx]);
            }

            _constructorBinders = binders;

            pipelineBuilder.Use(ToString(), PipelinePhase.Activation, MiddlewareInsertionMode.EndOfPhase, (ctxt, next) =>
            {
                ctxt.Instance = ActivateInstance(ctxt, ctxt.Parameters);

                next(ctxt);
            });
        }
示例#8
0
 public void BuildResolvePipeline(IComponentRegistryServices registryServices)
 {
     PipelineBuilding?.Invoke(this, new ResolvePipelineBuilder(PipelineType.Registration));
 }
 /// <inheritdoc/>
 public void ProvideMiddleware(Service service, IComponentRegistryServices availableServices, IResolvePipelineBuilder pipelineBuilder)
 {
     pipelineBuilder.UseRange(_componentRegistry.ServiceMiddlewareFor(service));
 }
 public void ConfigurePipeline(IComponentRegistryServices componentRegistryServices, IResolvePipelineBuilder pipelineBuilder)
 {
     // Should never be invoked.
     throw new InvalidOperationException();
 }
 /// <inheritdoc/>
 protected override IResolvePipeline BuildResolvePipeline(IComponentRegistryServices registryServices, IResolvePipelineBuilder pipelineBuilder)
 {
     // Just use the external pipeline.
     return(Target.ResolvePipeline);
 }