Example #1
0
        private object Activate(IEnumerable <Parameter> parameters)
        {
            ComponentRegistration.RaisePreparing(this, ref parameters);

            try
            {
                _newInstance = ComponentRegistration.Activator.ActivateInstance(this, parameters);
            }
            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.
                var instanceAsDisposable = _newInstance as IDisposable;
                if (instanceAsDisposable != null)
                {
                    _activationScope.Disposer.AddInstanceForDisposal(instanceAsDisposable);
                }
            }

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

            return(_newInstance);
        }
Example #2
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);
        }
Example #3
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);
        }
Example #4
0
        private object Activate(IEnumerable <Parameter> parameters)
        {
            ComponentRegistration.RaisePreparing(this, ref parameters);

            _newInstance = ComponentRegistration.Activator.ActivateInstance(this, parameters);

            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.
                var instanceAsDisposable = _newInstance as IDisposable;
                if (instanceAsDisposable != null)
                {
                    _activationScope.Disposer.AddInstanceForDisposal(instanceAsDisposable);
                }
            }

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

            return(_newInstance);
        }