private static object ApplyGraphScope(InitializationContext context, Func<object> getInstance)
        {
            var threadLocal = (ThreadLocal<Scope>)context.Registration.Container.GetItem(Key);

            var original = threadLocal.Value;

            try
            {
                threadLocal.Value = new Scope(context.Registration.Container);
                return getInstance();
            }
            finally
            {
                threadLocal.Value = original;
            }
        }
        private static object ApplyResolveScope(InitializationContext context, Func<object> getInstance)
        {
            var threadLocal = (ThreadLocal<Scope>)context.Registration.Container.GetItem(Key);

            var original = threadLocal.Value;
            var current = new Scope();

            try
            {
                threadLocal.Value = current;
                return getInstance();
            }
            finally
            {
                try
                {
                    current.Dispose();
                }
                finally
                {
                    threadLocal.Value = original;
                }
            }
        }
        internal InstanceProducer(Type serviceType, Registration registration, bool registerExternalProducer)
        {
            Requires.IsNotNull(serviceType, "serviceType");
            Requires.IsNotNull(registration, "registration");

            this.ServiceType = serviceType;
            this.Registration = registration;
            this.validator = new CyclicDependencyValidator(registration.ImplementationType);

            this.lazyExpression = new Lazy<Expression>(this.BuildExpressionInternal);
            this.initializationContext = new InitializationContext(this, registration);

            if (registerExternalProducer)
            {
                registration.Container.RegisterExternalProducer(this);
            }
        }
        internal InstanceProducer(Type serviceType, Registration registration, bool registerExternalProducer)
        {
            Requires.IsNotNull(serviceType, nameof(serviceType));
            Requires.IsNotNull(registration, nameof(registration));
            Requires.IsNotOpenGenericType(serviceType, nameof(serviceType));

            this.ServiceType = serviceType;
            this.Registration = registration;
            this.validator = new CyclicDependencyValidator(registration.ImplementationType);

            this.lazyExpression = new Lazy<Expression>(this.BuildExpressionInternal);
            this.initializationContext = new InitializationContext(this, registration);

            if (registerExternalProducer)
            {
                registration.Container.RegisterExternalProducer(this);
            }

            this.instanceCreator = this.BuildAndReplaceInstanceCreatorAndCreateFirstInstance;
        }