/// <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); }
/// <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); }
/// <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; } }