Ejemplo n.º 1
0
        private object Activate(IEnumerable <Parameter> parameters, out object decoratorTarget)
        {
            ComponentRegistration.RaisePreparing(this, ref parameters);

            var resolveParameters = parameters as Parameter[] ?? parameters.ToArray();

            try
            {
                decoratorTarget = _newInstance = ComponentRegistration.Activator.ActivateInstance(this, resolveParameters);

                ComponentRegistration.RaiseActivating(this, resolveParameters, ref _newInstance);

                _newInstance = InstanceDecorator.TryDecorateRegistration(
                    _service,
                    ComponentRegistration,
                    _newInstance,
                    _activationScope,
                    resolveParameters);
            }
            catch (ObjectDisposedException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw PropagateActivationException(this.ComponentRegistration.Activator, ex);
            }

            if (ComponentRegistration.Ownership == InstanceOwnership.OwnedByLifetimeScope)
            {
                // The fact this adds instances for disposal agnostic of the activator is
                // important. The ProvidedInstanceActivator will NOT dispose of the provided
                // instance once the instance has been activated - assuming that it will be
                // done during the lifetime scope's Disposer executing.
                if (decoratorTarget is IDisposable instanceAsDisposable)
                {
                    _activationScope.Disposer.AddInstanceForDisposal(instanceAsDisposable);
                }
                else if (decoratorTarget is IAsyncDisposable asyncDisposableInstance)
                {
                    _activationScope.Disposer.AddInstanceForAsyncDisposal(asyncDisposableInstance);
                }
            }

            if (_newInstance != decoratorTarget)
            {
                ComponentRegistration.RaiseActivating(this, resolveParameters, ref _newInstance);
            }

            return(_newInstance);
        }
Ejemplo n.º 2
0
        public object Execute()
        {
            if (_executed)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, ComponentActivationResources.ActivationAlreadyExecuted, this.ComponentRegistration));
            }

            _executed = true;

            object?decoratorTarget = null;

            var sharing = _decoratorTargetComponent != null
                ? _decoratorTargetComponent.Sharing
                : ComponentRegistration.Sharing;

            var resolveParameters = Parameters as Parameter[] ?? Parameters.ToArray();

            if (!_activationScope.TryGetSharedInstance(ComponentRegistration.Id, out _newInstance))
            {
                _newInstance = sharing == InstanceSharing.Shared
                    ? _activationScope.CreateSharedInstance(ComponentRegistration.Id, () => CreateInstance(Parameters))
                    : CreateInstance(Parameters);
            }

            decoratorTarget = _newInstance;

            _newInstance = InstanceDecorator.TryDecorateRegistration(
                _service,
                ComponentRegistration,
                _newInstance,
                _activationScope,
                resolveParameters);

            if (_newInstance != decoratorTarget)
            {
                ComponentRegistration.RaiseActivating(this, resolveParameters, ref _newInstance);
            }

            var handler = InstanceLookupEnding;

            handler?.Invoke(this, new InstanceLookupEndingEventArgs(this, NewInstanceActivated));

            StartStartableComponent(decoratorTarget);

            return(_newInstance);
        }
Ejemplo n.º 3
0
        private object Activate(IEnumerable <Parameter> parameters, out object decoratorTarget)
        {
            ComponentRegistration.RaisePreparing(this, ref parameters);

            var resolveParameters = parameters as Parameter[] ?? parameters.ToArray();

            try
            {
                decoratorTarget = _newInstance = ComponentRegistration.Activator.ActivateInstance(this, resolveParameters);

                _newInstance = InstanceDecorator.TryDecorateRegistration(
                    ComponentRegistration,
                    _newInstance,
                    _activationScope,
                    resolveParameters);
            }
            catch (Exception ex)
            {
                throw new DependencyResolutionException(String.Format(CultureInfo.CurrentCulture, ComponentActivationResources.ErrorDuringActivation, this.ComponentRegistration), ex);
            }

            if (ComponentRegistration.Ownership == InstanceOwnership.OwnedByLifetimeScope)
            {
                // The fact this adds instances for disposal agnostic of the activator is
                // important. The ProvidedInstanceActivator will NOT dispose of the provided
                // instance once the instance has been activated - assuming that it will be
                // done during the lifetime scope's Disposer executing.
                if (decoratorTarget is IDisposable instanceAsDisposable)
                {
                    _activationScope.Disposer.AddInstanceForDisposal(instanceAsDisposable);
                }
            }

            ComponentRegistration.RaiseActivating(this, resolveParameters, ref _newInstance);

            return(_newInstance);
        }