Exemple #1
0
        public override void PreBuildUp(ref BuilderContext context)
        {
            LifetimeManager policy = null;

            if (context.Registration is ContainerRegistration registration)
            {
                policy = registration.LifetimeManager;
            }

            if (null == policy || policy is PerResolveLifetimeManager)
            {
                policy = (LifetimeManager)context.Get(typeof(LifetimeManager));
            }
            if (null == policy)
            {
#if NETSTANDARD1_0 || NETCOREAPP1_0
                if (!context.RegistrationType.GetTypeInfo().IsGenericType)
                {
                    return;
                }
#else
                if (!context.RegistrationType.IsGenericType)
                {
                    return;
                }
#endif
                var manager = (LifetimeManager)context.Get(context.Type.GetGenericTypeDefinition(),
                                                           context.Name, typeof(LifetimeManager));
                if (null == manager)
                {
                    return;
                }

                lock (_genericLifetimeManagerLock)
                {
                    // check whether the policy for closed-generic has been added since first checked
                    policy = (LifetimeManager)context.Registration.Get(typeof(LifetimeManager));
                    if (null == policy)
                    {
                        policy = manager.CreateLifetimePolicy();
                        context.Registration.Set(typeof(LifetimeManager), policy);

                        if (policy is IDisposable)
                        {
                            context.Lifetime.Add(policy);
                        }
                    }
                }
            }

            if (policy is SynchronizedLifetimeManager recoveryPolicy)
            {
                context.RequiresRecovery = recoveryPolicy;
            }

            var existing = policy.GetValue(context.Lifetime);
            if (LifetimeManager.NoValue != existing)
            {
                context.Existing      = existing;
                context.BuildComplete = true;
            }
        }