Example #1
0
        private ILifetimePolicy GetLifetimePolicyForGenericType(IBuilderContext context)
        {
            Type   typeToBuild         = context.BuildKey.Type;
            object openGenericBuildKey = new NamedTypeBuildKey(typeToBuild.GetGenericTypeDefinition(),
                                                               context.BuildKey.Name);

            IPolicyList            factorySource;
            ILifetimeFactoryPolicy factoryPolicy =
                context.Policies.Get <ILifetimeFactoryPolicy>(openGenericBuildKey, out factorySource);

            if (factoryPolicy != null)
            {
                // creating the lifetime policy can result in arbitrary code execution
                // in particular it will likely result in a Resolve call, which could result in locking
                // to avoid deadlocks the new lifetime policy is created outside the lock
                // multiple instances might be created, but only one instance will be used
                ILifetimePolicy newLifetime = factoryPolicy.CreateLifetimePolicy();

                lock (this.genericLifetimeManagerLock)
                {
                    // check whether the policy for closed-generic has been added since first checked
                    ILifetimePolicy lifetime = factorySource.GetNoDefault <ILifetimePolicy>(context.BuildKey, false);
                    if (lifetime == null)
                    {
                        factorySource.Set <ILifetimePolicy>(newLifetime, context.BuildKey);
                        lifetime = newLifetime;
                    }

                    return(lifetime);
                }
            }

            return(null);
        }
Example #2
0
        private static ILifetimePolicy GetLifetimePolicyForGenericType(IBuilderContext context)
        {
            Type   typeToBuild         = BuildKey.GetType(context.BuildKey);
            object openGenericBuildKey =
                BuildKey.ReplaceType(context.BuildKey, typeToBuild.GetGenericTypeDefinition());

            ILifetimeFactoryPolicy factoryPolicy =
                context.Policies.Get <ILifetimeFactoryPolicy>(openGenericBuildKey);

            if (factoryPolicy != null)
            {
                ILifetimePolicy lifetime = factoryPolicy.CreateLifetimePolicy();
                context.PersistentPolicies.Set <ILifetimePolicy>(lifetime, context.BuildKey);
                return(lifetime);
            }

            return(null);
        }
        private static ILifetimePolicy GetLifetimePolicyForGenericType(IBuilderContext context)
        {
            Type   typeToBuild         = context.BuildKey.Type;
            object openGenericBuildKey = new NamedTypeBuildKey(typeToBuild.GetGenericTypeDefinition(),
                                                               context.BuildKey.Name);

            IPolicyList            factorySource;
            ILifetimeFactoryPolicy factoryPolicy =
                context.Policies.Get <ILifetimeFactoryPolicy>(openGenericBuildKey, out factorySource);

            if (factoryPolicy != null)
            {
                ILifetimePolicy lifetime = factoryPolicy.CreateLifetimePolicy();
                factorySource.Set <ILifetimePolicy>(lifetime, context.BuildKey);
                return(lifetime);
            }

            return(null);
        }
Example #4
0
 /// <summary>
 /// Create a new <see cref="LifetimeManagerFactory"/> that will
 /// return instances of the given type, creating them by
 /// resolving through the container.
 /// </summary>
 /// <param name="policy">LifetimeManager to create.</param>
 public LifetimeManagerFactory(ExtensionContext containerContext, ILifetimeFactoryPolicy policy)
 {
     _containerContext = containerContext;
     _policy           = policy ?? throw new ArgumentNullException(nameof(policy));
     LifetimeType      = policy.GetType();
 }