示例#1
0
        /// <summary>
        /// Continue building the object graph by instantiating <paramref name="registration"/> in the
        /// current <paramref name="currentOperationScope"/>.
        /// </summary>
        /// <param name="currentOperationScope">The current scope of the operation.</param>
        /// <param name="registration">The component to activate.</param>
        /// <param name="parameters">The parameters for the component.</param>
        /// <returns>The resolved instance.</returns>
        /// <exception cref="ArgumentNullException"/>
        public object GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable <Parameter> parameters)
        {
            if (_ended)
            {
                throw new ObjectDisposedException(ResolveOperationResources.TemporaryContextDisposed, innerException: null);
            }

            CircularDependencyDetector.CheckForCircularDependency(registration, _activationStack, ++_callDepth);

            var activation = new InstanceLookup(registration, this, currentOperationScope, parameters);

            _activationStack.Push(activation);

            var handler = InstanceLookupBeginning;

            handler?.Invoke(this, new InstanceLookupBeginningEventArgs(activation));

            var instance = activation.Execute();

            _successfulActivations.Add(activation);

            _activationStack.Pop();

            if (_activationStack.Count == 0)
            {
                CompleteActivations();
            }

            --_callDepth;

            return(instance);
        }
示例#2
0
        /// <summary>
        /// Continue building the object graph by instantiating <paramref name="registration"/> in the
        /// current <paramref name="currentOperationScope"/>.
        /// </summary>
        /// <param name="currentOperationScope">The current scope of the operation.</param>
        /// <param name="registration">The component to activate.</param>
        /// <param name="parameters">The parameters for the component.</param>
        /// <returns>The resolved instance.</returns>
        /// <exception cref="ArgumentNullException"/>
        public object GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable <Parameter> parameters)
        {
            if (_ended)
            {
                throw new ObjectDisposedException(ResolveOperationResources.TemporaryContextDisposed, innerException: null);
            }

            CircularDependencyDetector.CheckForCircularDependency(registration, _activationStack, ++_callDepth);

            var activation = new InstanceLookup(registration, this, currentOperationScope, parameters);

            // Do not add to the circular dependency resolution stack unless the activator is a reflection activator (ie: generates the actual instance)
            bool isRealActivation = (activation.ComponentRegistration.Activator is ReflectionActivator);

            if (isRealActivation)
            {
                _activationStack.Push(activation);
            }

            // Count the number of activations so we know when the last one is completed. We need to keep track of all activations, not
            // just reflection activations here
            _activationCount++;

            var handler = InstanceLookupBeginning;

            if (handler != null)
            {
                handler(this, new InstanceLookupBeginningEventArgs(activation));
            }

            var instance = activation.Execute();

            _successfulActivations.Add(activation);

            // Do not pop unless we pushed it above
            if (isRealActivation)
            {
                _activationStack.Pop();
            }

            // Decrement the activation count
            _activationCount--;

            // Inject properties AFTER the item is removed from the circular dependency stack, so property-property circular dependencies always work
            activation.InjectProperties();

            // Complete activations if this is the last recursive item in the activation list
            if (_activationCount == 0)
            {
                CompleteActivations();
            }

            --_callDepth;

            return(instance);
        }
示例#3
0
        /// <summary>
        /// Continue building the object graph by instantiating <paramref name="registration"/> in the
        /// current <paramref name="currentOperationScope"/>.
        /// </summary>
        /// <param name="currentOperationScope">The current scope of the operation.</param>
        /// <param name="registration">The component to activate.</param>
        /// <param name="parameters">The parameters for the component.</param>
        /// <returns>The resolved instance.</returns>
        /// <exception cref="ArgumentNullException"/>
        public object GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable <Parameter> parameters)
        {
            if (_ended)
            {
                throw new ObjectDisposedException(ResolveOperationResources.TemporaryContextDisposed, innerException: null);
            }

            ++_callDepth;

            if (_activationStack.Count > 0)
            {
                CircularDependencyDetector.CheckForCircularDependency(registration, _activationStack, _callDepth);
            }

            var activation = new InstanceLookup(registration, this, currentOperationScope, parameters);

            _activationStack.Push(activation);

            var handler = InstanceLookupBeginning;

            handler?.Invoke(this, new InstanceLookupBeginningEventArgs(activation));

            try
            {
                var instance = activation.Execute();
                _successfulActivations.Add(activation);

                return(instance);
            }
            finally
            {
                // Issue #929: Allow the activation stack to be popped even if the activation failed.
                // This allows try/catch to happen in lambda registrations without corrupting the stack.
                _activationStack.Pop();

                if (_activationStack.Count == 0)
                {
                    CompleteActivations();
                }

                --_callDepth;
            }
        }
示例#4
0
        public object GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable <Parameter> parameters)
        {
            if (currentOperationScope == null)
            {
                throw new ArgumentNullException("currentOperationScope");
            }
            if (registration == null)
            {
                throw new ArgumentNullException("registration");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            if (this._ended)
            {
                Exception innerException = null;
                throw new ObjectDisposedException(ResolveOperationResources.TemporaryContextDisposed, innerException);
            }
            this._circularDependencyDetector.CheckForCircularDependency(registration, this._activationStack, ++this._callDepth);
            InstanceLookup item = new InstanceLookup(registration, this, currentOperationScope, parameters);

            this._activationStack.Push(item);
            EventHandler <InstanceLookupBeginningEventArgs> instanceLookupBeginning = this.InstanceLookupBeginning;

            if (instanceLookupBeginning != null)
            {
                instanceLookupBeginning(this, new InstanceLookupBeginningEventArgs(item));
            }
            object obj2 = item.Execute();

            this._successfulActivations.Add(item);
            this._activationStack.Pop();
            if (this._activationStack.Count == 0)
            {
                this.CompleteActivations();
            }
            this._callDepth--;
            return(obj2);
        }
        /// <summary>
        /// Continue building the object graph by instantiating <paramref name="registration"/> in the
        /// current <paramref name="currentOperationScope"/>.
        /// </summary>
        /// <param name="currentOperationScope">The current scope of the operation.</param>
        /// <param name="registration">The component to activate.</param>
        /// <param name="parameters">The parameters for the component.</param>
        /// <returns>The resolved instance.</returns>
        /// <exception cref="ArgumentNullException"/>
        public object GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable<Parameter> parameters)
        {
            if (currentOperationScope == null) throw new ArgumentNullException("currentOperationScope");
            if (registration == null) throw new ArgumentNullException("registration");
            if (parameters == null) throw new ArgumentNullException("parameters");
            if (_ended) throw new ObjectDisposedException(ResolveOperationResources.TemporaryContextDisposed, innerException: null);

            _circularDependencyDetector.CheckForCircularDependency(registration, _activationStack, ++_callDepth);

            var activation = new InstanceLookup(registration, this, currentOperationScope, parameters);

            _activationStack.Push(activation);

            var handler = InstanceLookupBeginning;
            if (handler != null)
                handler(this, new InstanceLookupBeginningEventArgs(activation));

            var instance = activation.Execute();
            _successfulActivations.Add(activation);

            _activationStack.Pop();

            if (_activationStack.Count == 0)
                CompleteActivations();

            --_callDepth;

            return instance;
        }