Beispiel #1
0
        internal LifetimeScope(IComponentRegistry registry, ISharingLifetimeScope parentLifetimeScope)
            : this(registry)
        {
            RootLifetimeScope = parentLifetimeScope.RootLifetimeScope;

            ParentLifetimeScope = parentLifetimeScope;
        }
Beispiel #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);

            _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);
        }
Beispiel #3
0
 /// <summary>
 /// Create a root lifetime scope for the provided components.
 /// </summary>
 /// <param name="tag">The tag applied to the <see cref="ILifetimeScope"/>.</param>
 /// <param name="componentRegistry">Components used in the scope.</param>
 public LifetimeScope(IComponentRegistry componentRegistry, object tag)
     : this()
 {
     _componentRegistry = Enforce.ArgumentNotNull(componentRegistry, "componentRegistry");
     _root = this;
     _tag  = Enforce.ArgumentNotNull(tag, "tag");
 }
Beispiel #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ResolveOperation" /> class.
 /// </summary>
 /// <param name="mostNestedLifetimeScope"> The most nested scope in which to begin the operation. The operation
 /// can move upward to less nested scopes as components with wider sharing scopes are activated.
 /// </param>
 /// <param name="diagnosticSource">
 /// The <see cref="System.Diagnostics.DiagnosticListener" /> to which trace events should be written.
 /// </param>
 public ResolveOperation(
     ISharingLifetimeScope mostNestedLifetimeScope,
     DiagnosticListener diagnosticSource)
 {
     CurrentScope     = mostNestedLifetimeScope ?? throw new ArgumentNullException(nameof(mostNestedLifetimeScope));
     DiagnosticSource = diagnosticSource ?? throw new ArgumentNullException(nameof(diagnosticSource));
 }
Beispiel #5
0
        public InstanceLookup(
            IResolveOperation context,
            ISharingLifetimeScope mostNestedVisibleScope,
            ResolveRequest request)
        {
            _context = context;
            _service = request.Service;
            _decoratorTargetComponent = request.DecoratorTarget;
            ComponentRegistration     = request.Registration;
            Parameters = request.Parameters;

            try
            {
                _activationScope = ComponentRegistration.Lifetime.FindScope(mostNestedVisibleScope);
            }
            catch (DependencyResolutionException ex)
            {
                var services = new StringBuilder();
                foreach (var s in ComponentRegistration.Services)
                {
                    services.Append("- ");
                    services.AppendLine(s.Description);
                }

                var message = string.Format(CultureInfo.CurrentCulture, ComponentActivationResources.UnableToLocateLifetimeScope, ComponentRegistration.Activator.LimitType, services);
                throw new DependencyResolutionException(message, ex);
            }
        }
 public InstanceLookup(IComponentRegistration registration, IResolveOperation context, ISharingLifetimeScope mostNestedVisibleScope, IEnumerable <Parameter> parameters)
 {
     this._parameters            = parameters;
     this._componentRegistration = registration;
     this._context         = context;
     this._activationScope = this._componentRegistration.Lifetime.FindScope(mostNestedVisibleScope);
 }
        internal override void DoBuildInstance(ISharingLifetimeScope scope, ParameterSet parameters, out T instance)
        {
            var context = new NonViralSharedInjectionContext <T>(scope, _description, parameters);

            _process.Execute(context);
            instance = context.Instance;
        }
Beispiel #8
0
        public InstanceLookup(
            IComponentRegistration registration,
            IResolveOperation context,
            ISharingLifetimeScope mostNestedVisibleScope,
            IEnumerable <Parameter> parameters)
        {
            Parameters            = parameters;
            ComponentRegistration = registration;
            _context = context;

            try
            {
                _activationScope = ComponentRegistration.Lifetime.FindScope(mostNestedVisibleScope);
            }
            catch (DependencyResolutionException ex)
            {
                var services = new StringBuilder();
                foreach (var s in registration.Services)
                {
                    services.Append("- ");
                    services.AppendLine(s.Description);
                }

                var message = String.Format(CultureInfo.CurrentCulture, ComponentActivationResources.UnableToLocateLifetimeScope, registration.Activator.LimitType, services);
                throw new DependencyResolutionException(message, ex);
            }
        }
        // A lock is only needed when the first time an instance is built
        internal override void DoBuildInstance(ISharingLifetimeScope scope, ParameterSet parameters, out T instance)
        {
            var injectionOperator = _builder.InjectionOperator;

            if (!ReferenceEquals(injectionOperator, this))
            {
                _builder.BuildInstance(scope, parameters, out instance);
                return;
            }

            Monitor.Enter(_builder.ObjectRelation.SyncRoot);
            injectionOperator = _builder.InjectionOperator;
            if (!ReferenceEquals(injectionOperator, this))
            {
                Monitor.Exit(_builder.ObjectRelation.SyncRoot);
                _builder.BuildInstance(scope, parameters, out instance);
                return;
            }

            try
            {
                var process = _configurationSet.CreateInjectionProcess <T>(scope.Kernel);
                var oneoff  = new OneOffInjectionOperator <T>(_builder, _description, _lifetime, process);
                InjectionOperatorHelper.UpgradeToOneOffObjectBuilder(_builder, oneoff);
                oneoff.BuildInstance(scope, parameters, out instance);
            }
            finally
            {
                Monitor.Exit(_builder.ObjectRelation.SyncRoot);
            }
        }
Beispiel #10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ResolveOperation"/> class.
        /// </summary>
        /// <param name="mostNestedLifetimeScope">The most nested scope in which to begin the operation. The operation
        /// can move upward to less nested scopes as components with wider sharing scopes are activated.</param>
        public ResolveOperation(ISharingLifetimeScope mostNestedLifetimeScope)
        {
            _mostNestedLifetimeScope = mostNestedLifetimeScope;

            // Initialise _successfulActivations.
            ResetSuccessfulActivations();
        }
Beispiel #11
0
 /// <summary>
 /// Create a root lifetime scope for the provided components.
 /// </summary>
 /// <param name="tag">The tag applied to the <see cref="ILifetimeScope"/>.</param>
 /// <param name="componentRegistry">Components used in the scope.</param>
 public LifetimeScope(IComponentRegistry componentRegistry, object tag)
     : this()
 {
     _componentRegistry = Enforce.ArgumentNotNull(componentRegistry, "componentRegistry");
     _root = this;
     _tag = Enforce.ArgumentNotNull(tag, "tag");
 }
        protected T DoBuildInstance(ISharingLifetimeScope scope, InjectionOperator <T> injectionOperator, ParameterSet parameters)
        {
            T instance;

            injectionOperator.DoBuildInstance(scope, parameters, out instance);
            return(instance);
        }
Beispiel #13
0
        public InstanceLookup(
            IComponentRegistration registration,
            IResolveOperation context,
            ISharingLifetimeScope mostNestedVisibleScope,
            IEnumerable<Parameter> parameters)
        {
            Parameters = parameters;
            ComponentRegistration = registration;
            _context = context;

            try
            {
                _activationScope = ComponentRegistration.Lifetime.FindScope(mostNestedVisibleScope);
            }
            catch (DependencyResolutionException ex)
            {
                var services = new StringBuilder();
                foreach (var s in registration.Services)
                {
                    services.Append("- ");
                    services.AppendLine(s.Description);
                }

                var message = String.Format(CultureInfo.CurrentCulture, ComponentActivationResources.UnableToLocateLifetimeScope, registration.Activator.LimitType, services);
                throw new DependencyResolutionException(message, ex);
            }
        }
Beispiel #14
0
 /// <summary>
 /// Given the most nested scope visible within the resolve operation, find
 /// the scope for the component.
 /// </summary>
 /// <param name="mostNestedVisibleScope">The most nested visible scope.</param>
 /// <returns>The scope for the component.</returns>
 public ISharingLifetimeScope FindScope(ISharingLifetimeScope mostNestedVisibleScope)
 {
     if (mostNestedVisibleScope == null)
     {
         throw new ArgumentNullException("mostNestedVisibleScope");
     }
     return(mostNestedVisibleScope.RootLifetimeScope);
 }
 public ISharingLifetimeScope FindScope(ISharingLifetimeScope mostNestedVisibleScope)
 {
     if (_threadScope.Value == null)
     {
         _threadScope.Value = new LifetimeScope(mostNestedVisibleScope.ComponentRegistry);
     }
     return((ISharingLifetimeScope)_threadScope.Value);
 }
Beispiel #16
0
 public InProcessPipline(ILifetimeScope lifetimeScope, Func <T, ILifetimeScope, Task> eventHandler)
 {
     this.Pipline      = Channel.CreateUnbounded <T>();
     this.EventHandler = eventHandler;
     Container         = ((LifetimeScope)lifetimeScope).RootLifetimeScope;
     logger            = lifetimeScope.Resolve <ILogger>();
     _ = SubscribeHandleInvoke();
 }
        internal InjectionContext(InjectionContext context, ObjectDescription description, ParameterSet parameters)
        {
            _parentContext = context;
            _scope         = context.LifetimeScope;

            _description = description;
            _parameters  = parameters;
        }
Beispiel #18
0
        /// <summary>
        /// Create a lifetime scope for the provided components and nested beneath a parent.
        /// </summary>
        /// <param name="tag">The tag applied to the <see cref="ILifetimeScope"/>.</param>
        /// <param name="componentRegistry">Components used in the scope.</param>
        /// <param name="parent">Parent scope.</param>
        protected LifetimeScope(IComponentRegistry componentRegistry, LifetimeScope parent, object tag)
            : this(componentRegistry, tag)
        {
            if (parent == null) throw new ArgumentNullException(nameof(parent));

            _parent = parent;
            RootLifetimeScope = _parent.RootLifetimeScope;
        }
Beispiel #19
0
 public override T BuildInstance(ISharingLifetimeScope scope, InjectionOperator <T> injectionOperator, ParameterSet parameters)
 {
     if (injectionOperator.Resolved)
     {
         return(_instance);
     }
     _instance = BuildSingletonInstance(scope, injectionOperator, parameters);
     return(_instance);
 }
Beispiel #20
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);
        }
        protected override T BuildSingletonInstance(ISharingLifetimeScope scope, InjectionOperator <T> injectionOperator, ParameterSet parameters)
        {
            var instance      = DoBuildInstance(scope, injectionOperator, parameters);
            var disposable    = instance as IDisposable;
            var matchingScope = scope.ContainerScope;

            lock (matchingScope.SyncRoot)
                matchingScope.RegisterForDisposal(disposable);
            return(instance);
        }
        internal override void DoBuildInstance(ISharingLifetimeScope scope, ParameterSet parameters, out T instance)
        {
            var context = new ViralSharedInjectionContext <T>(scope, _description, parameters);

            _process.Execute(context);
            instance = context.Instance;
            // replace the ObjectBuilder
            InjectionOperatorHelper.UpgradeToNonSharedObjectBuilder
                (_builder, new NonSharedInjectionOperator <T>(_builder, _description, _lifetime, _process));
        }
Beispiel #23
0
 public InstanceLookup(
     IComponentRegistration registration,
     IResolveOperation context,
     ISharingLifetimeScope mostNestedVisibleScope,
     IEnumerable<Parameter> parameters)
 {
     Parameters = parameters;
     ComponentRegistration = registration;
     _context = context;
     _activationScope = ComponentRegistration.Lifetime.FindScope(mostNestedVisibleScope);
 }
Beispiel #24
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LifetimeScope"/> class.
        /// </summary>
        /// <param name="tag">The tag applied to the <see cref="ILifetimeScope"/>.</param>
        /// <param name="componentRegistry">Components used in the scope.</param>
        /// <param name="parent">Parent scope.</param>
        protected LifetimeScope(IComponentRegistry componentRegistry, LifetimeScope parent, object tag)
            : this(componentRegistry, tag)
        {
            if (parent == null)
            {
                throw new ArgumentNullException(nameof(parent));
            }

            _parent           = parent;
            RootLifetimeScope = _parent.RootLifetimeScope;
        }
        public override T BuildInstance(ISharingLifetimeScope scope, InjectionOperator <T> injectionOperator, ParameterSet parameters)
        {
            var matchingScope = scope.SharingScope;

            Lifetime.ThrowWhenMatchingScopeIsNull(matchingScope, _error);
            var instance   = DoBuildInstance(scope, injectionOperator, parameters);
            var disposable = instance as IDisposable;

            lock (matchingScope.SyncRoot)
                matchingScope.RegisterForDisposal(disposable);
            return(instance);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="DefaultResolveRequestContext" /> class.
 /// </summary>
 /// <param name="owningOperation">The owning resolve operation.</param>
 /// <param name="request">The initiating resolve request.</param>
 /// <param name="scope">The lifetime scope.</param>
 /// <param name="diagnosticSource">
 /// The <see cref="System.Diagnostics.DiagnosticListener" /> to which trace events should be written.
 /// </param>
 internal DefaultResolveRequestContext(
     IResolveOperation owningOperation,
     ResolveRequest request,
     ISharingLifetimeScope scope,
     DiagnosticListener diagnosticSource)
 {
     Operation        = owningOperation;
     ActivationScope  = scope;
     Parameters       = request.Parameters;
     _resolveRequest  = request ?? throw new ArgumentNullException(nameof(request));
     PhaseReached     = PipelinePhase.ResolveRequestStart;
     DiagnosticSource = diagnosticSource;
 }
Beispiel #27
0
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            _currentResolutionScope = _parent;
            if (disposing)
            {
                _disposer.Dispose();
                _serviceCache.Clear();
                lock (_synchRoot)
                    _sharedInstances.Clear();
            }

            base.Dispose(disposing);
        }
Beispiel #28
0
		//internal readonly int ThreadID;

		public InstanceLookup(
			Service service,
			IComponentRegistration registration,
			IComponentContext scope,
			ISharingLifetimeScope mostNestedVisibleScope,
			IEnumerable<Parameter> parameters)
		{
			_service = service;
			_parameters = parameters;
			_componentRegistration = registration;
			_scope = scope;
			_activationScope = _componentRegistration.Lifetime.FindScope(mostNestedVisibleScope);
			//ThreadID = Thread.CurrentThread.ManagedThreadId;
		}
Beispiel #29
0
        //internal readonly int ThreadID;

        public InstanceLookup(
            Service service,
            IComponentRegistration registration,
            IComponentContext scope,
            ISharingLifetimeScope mostNestedVisibleScope,
            IEnumerable <Parameter> parameters)
        {
            _service               = service;
            _parameters            = parameters;
            _componentRegistration = registration;
            _scope           = scope;
            _activationScope = _componentRegistration.Lifetime.FindScope(mostNestedVisibleScope);
            //ThreadID = Thread.CurrentThread.ManagedThreadId;
        }
 public ISharingLifetimeScope FindScope(ISharingLifetimeScope mostNestedVisibleScope)
 {
     if (mostNestedVisibleScope == null)
     {
         throw new ArgumentNullException("mostNestedVisibleScope");
     }
     for (ISharingLifetimeScope scope = mostNestedVisibleScope; scope != null; scope = scope.ParentLifetimeScope)
     {
         if (this._matcher(scope))
         {
             return(scope);
         }
     }
     throw new DependencyResolutionException(string.Format(CultureInfo.CurrentCulture, MatchingScopeLifetimeResources.MatchingScopeNotFound, new object[] { this._matchExpressionCode }));
 }
Beispiel #31
0
        private void CheckTagIsUnique(object tag)
        {
            ISharingLifetimeScope parentScope = this;

            while (parentScope != RootLifetimeScope)
            {
                if (parentScope.Tag.Equals(tag))
                {
                    throw new InvalidOperationException(
                              string.Format(CultureInfo.CurrentCulture, LifetimeScopeResources.DuplicateTagDetected, tag));
                }

                parentScope = parentScope.ParentLifetimeScope;
            }
        }
Beispiel #32
0
        /// <summary>
        /// Given the most nested scope visible within the resolve operation, find
        /// the scope for the component.
        /// </summary>
        /// <param name="mostNestedVisibleScope">The most nested visible scope.</param>
        /// <returns>The scope for the component.</returns>
        public ISharingLifetimeScope FindScope(ISharingLifetimeScope mostNestedVisibleScope)
        {
            if (mostNestedVisibleScope == null) throw new ArgumentNullException("mostNestedVisibleScope");

            var next = mostNestedVisibleScope;
            while (next != null)
            {
                if (_matcher(next))
                    return next;

                next = next.ParentLifetimeScope;
            }

            throw new DependencyResolutionException(string.Format(
                CultureInfo.CurrentCulture, MatchingScopeLifetimeResources.MatchingScopeNotFound, _matchExpressionCode));
        }
        /// <summary>
        /// Given the most nested scope visible within the resolve operation, find
        /// the scope for the component.
        /// </summary>
        /// <param name="mostNestedVisibleScope">The most nested visible scope.</param>
        /// <returns>The scope for the component.</returns>
        public ISharingLifetimeScope FindScope(ISharingLifetimeScope mostNestedVisibleScope)
        {
            if (mostNestedVisibleScope == null) throw new ArgumentNullException("mostNestedVisibleScope");

            var next = mostNestedVisibleScope;
            while (next != null)
            {
                if (_tagsToMatch.Contains(next.Tag))
                    return next;

                next = next.ParentLifetimeScope;
            }

            throw new DependencyResolutionException(string.Format(
                CultureInfo.CurrentCulture, MatchingScopeLifetimeResources.MatchingScopeNotFound, string.Join(", ", _tagsToMatch)));
        }
Beispiel #34
0
    public ISharingLifetimeScope FindScope(ISharingLifetimeScope mostNestedVisibleScope)
    {
        if (mostNestedVisibleScope == null)
        {
            throw new ArgumentNullException("mostNestedVisibleScope");
        }
        var next = mostNestedVisibleScope;

        while (next != null)
        {
            if (_tagsToMatch.Contains(next.Tag))
            {
                return(next);
            }
            next = next.ParentLifetimeScope;
        }
        return(mostNestedVisibleScope.RootLifetimeScope);
    }
Beispiel #35
0
        public override T BuildInstance(ISharingLifetimeScope scope, InjectionOperator <T> injectionOperator, ParameterSet parameters)
        {
            var matchingScope = scope.SharingScope;

            Lifetime.ThrowWhenMatchingScopeIsNull(matchingScope, _error);
            lock (matchingScope.SyncRoot)
            {
                var cachedInstance = matchingScope.GetInstance(injectionOperator.ObjectDescription);
                if (cachedInstance != null)
                {
                    return((T)cachedInstance);
                }

                var instance = DoBuildInstance(scope, injectionOperator, parameters);
                matchingScope.SetInstance(injectionOperator.ObjectDescription, instance);
                return(instance);
            }
        }
Beispiel #36
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;
            }
        }
        public ISharingLifetimeScope FindScope(ISharingLifetimeScope mostNestedVisibleScope)
        {
#if NetCore
            var context = _container.GetInstance <IHttpContextAccessor>()?.HttpContext;
            if (context == null)
            {
                return(null);
            }
            if (!context.Items.ContainsKey(typeof(IComponentLifetime)))
            {
                lock (context)
                {
                    if (!context.Items.ContainsKey(typeof(IComponentLifetime)))
                    {
                        var scope = (ISharingLifetimeScope)mostNestedVisibleScope.BeginLifetimeScope(mostNestedVisibleScope.ComponentRegistry);
                        context.Items[typeof(IComponentLifetime)] = scope;
                        return(scope);
                    }
                }
            }
            return((ISharingLifetimeScope)context.Items[typeof(IComponentLifetime)]);
#else
            var context = System.Web.HttpContext.Current;
            if (context == null)
            {
                return(null);
            }
            if (!context.Items.Contains(typeof(IComponentLifetime)))
            {
                lock (context.Items.SyncRoot)
                {
                    if (!context.Items.Contains(typeof(IComponentLifetime)))
                    {
                        var scope = (ISharingLifetimeScope)mostNestedVisibleScope.BeginLifetimeScope(mostNestedVisibleScope.ComponentRegistry);
                        context.Items[typeof(IComponentLifetime)] = scope;
                        context.DisposeOnPipelineCompleted(scope);
                        return(scope);
                    }
                }
            }
            return((ISharingLifetimeScope)context.Items[typeof(IComponentLifetime)]);
#endif
        }
Beispiel #38
0
		/// <summary>
		/// Releases unmanaged and - optionally - managed resources
		/// </summary>
		/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
		protected override void Dispose(bool disposing)
		{
			_currentResolutionScope = _parent;
			if (disposing)
			{
				_disposer.Dispose();
				_serviceCache.Clear();
				lock (_synchRoot)
					_sharedInstances.Clear();
			}

			base.Dispose(disposing);
		}
Beispiel #39
0
 /// <summary>
 /// Create a lifetime scope for the provided components and nested beneath a parent.
 /// </summary>
 /// <param name="tag">The tag applied to the <see cref="ILifetimeScope"/>.</param>
 /// <param name="componentRegistry">Components used in the scope.</param>
 /// <param name="parent">Parent scope.</param>
 protected LifetimeScope(IComponentRegistry componentRegistry, LifetimeScope parent, object tag)
     : this(componentRegistry, tag)
 {
     _parent = Enforce.ArgumentNotNull(parent, "parent");
     _root = _parent.RootLifetimeScope;
 }
 /// <summary>
 /// Create an instance of <see cref="ResolveOperation"/> in the provided scope.
 /// </summary>
 /// <param name="mostNestedLifetimeScope">The most nested scope in which to begin the operation. The operation
 /// can move upward to less nested scopes as components with wider sharing scopes are activated</param>
 public ResolveOperation(ISharingLifetimeScope mostNestedLifetimeScope)
 {
     _mostNestedLifetimeScope = mostNestedLifetimeScope;
     ResetSuccessfulActivations();
 }
        /// <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;
        }
Beispiel #42
0
		private LifetimeScope()
		{
			_sharedInstances.Add(SelfRegistrationId, this);
			_currentResolutionScope = this;
		}
Beispiel #43
0
        /// <summary>
        /// Given the most nested scope visible within the resolve operation, find
        /// the scope for the component.
        /// </summary>
        /// <param name="mostNestedVisibleScope">The most nested visible scope.</param>
        /// <returns>The scope for the component.</returns>
        public ISharingLifetimeScope FindScope(ISharingLifetimeScope mostNestedVisibleScope)
        {
            if (mostNestedVisibleScope == null) throw new ArgumentNullException("mostNestedVisibleScope");

            var next = mostNestedVisibleScope;
            while (next != null)
            {
                if (_matcher(next))
                    return next;

                next = next.ParentLifetimeScope;
            }

            throw new DependencyResolutionException(string.Format(
                CultureInfo.CurrentCulture, "No scope with a Tag matching '{0}' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being reqested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself.", _matchExpressionCode));
        }
Beispiel #44
0
 /// <summary>
 /// Given the most nested scope visible within the resolve operation, find
 /// the scope for the component.
 /// </summary>
 /// <param name="mostNestedVisibleScope">The most nested visible scope.</param>
 /// <returns>The scope for the component.</returns>
 public ISharingLifetimeScope FindScope(ISharingLifetimeScope mostNestedVisibleScope)
 {
     if (mostNestedVisibleScope == null) throw new ArgumentNullException("mostNestedVisibleScope");
     return mostNestedVisibleScope.RootLifetimeScope;
 }