/// <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); } }
/// <summary> /// Use a middleware callback in a resolve pipeline. /// </summary> /// <param name="builder">The container builder.</param> /// <param name="descriptor">A description for the middleware; this will show up in any resolve tracing.</param> /// <param name="phase">The phase of the pipeline the middleware should run at.</param> /// <param name="callback"> /// A callback invoked to run your middleware. /// This callback takes a <see cref="ResolveRequestContext"/>, containing the context for the resolve request, plus /// a callback to invoke to continue the pipeline. /// </param> /// <returns>The same builder instance.</returns> public static IResolvePipelineBuilder Use(this IResolvePipelineBuilder builder, string descriptor, PipelinePhase phase, Action <ResolveRequestContext, Action <ResolveRequestContext> > callback) { if (builder is null) { throw new ArgumentNullException(nameof(builder)); } builder.Use(new DelegateMiddleware(descriptor, phase, callback), MiddlewareInsertionMode.EndOfPhase); return(builder); }
/// <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); }); }
private void RegistrationPipelineBuilding(object sender, IResolvePipelineBuilder pipeline) { pipeline.Use(PipelinePhase.Activation, MiddlewareInsertionMode.EndOfPhase, (c, next) => { c.ChangeParameters(c.Parameters.Union(new[] { new ResolvedParameter( (p, i) => p.ParameterType == typeof(ILogger), (p, i) => new Log4Net(p.Member.DeclaringType) ) })); next(c); }); }
/// <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; }); }
/// <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; } }); }
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); } }); }
/// <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); }); }
private void RegistrationPipelineBuilding(object sender, IResolvePipelineBuilder builder) { builder.Use(new PropertyAutoWriteMiddleware()); }
/// <summary> /// Use a middleware callback in a resolve pipeline. /// </summary> /// <param name="builder">The container builder.</param> /// <param name="phase">The phase of the pipeline the middleware should run at.</param> /// <param name="insertionMode">The insertion mode specifying whether to add at the start or end of the phase.</param> /// <param name="callback"> /// A callback invoked to run your middleware. /// This callback takes a <see cref="ResolveRequestContext"/>, containing the context for the resolve request, plus /// a callback to invoke to continue the pipeline. /// </param> /// <returns>The same builder instance.</returns> public static IResolvePipelineBuilder Use(this IResolvePipelineBuilder builder, PipelinePhase phase, MiddlewareInsertionMode insertionMode, Action <ResolveRequestContext, Action <ResolveRequestContext> > callback) { builder.Use(AnonymousDescriptor, phase, insertionMode, callback); return(builder); }
/// <summary> /// Use a middleware callback in a resolve pipeline. /// </summary> /// <param name="builder">The container builder.</param> /// <param name="phase">The phase of the pipeline the middleware should run at.</param> /// <param name="callback"> /// A callback invoked to run your middleware. /// This callback takes a <see cref="ResolveRequestContext"/>, containing the context for the resolve request, plus /// a callback to invoke to continue the pipeline. /// </param> /// <returns>The same builder instance.</returns> public static IResolvePipelineBuilder Use(this IResolvePipelineBuilder builder, PipelinePhase phase, Action <ResolveRequestContext, Action <ResolveRequestContext> > callback) { builder.Use(phase, MiddlewareInsertionMode.EndOfPhase, callback); return(builder); }