コード例 #1
0
        /// <inheritdoc />
        Task <object?> IUnityContainerAsync.Resolve(Type type, string?name, params ResolverOverride[] overrides)
        {
            // Setup Context
            var registration = GetRegistration(type ?? throw new ArgumentNullException(nameof(type)), name);

            // Check if pipeline exists
            if (null == registration.Pipeline)
            {
                // Start a new Task to create and execute pipeline
                return(Task.Factory.StartNew(() =>
                {
                    var context = new BuilderContext
                    {
                        List = new PolicyList(),
                        Type = type,
                        Overrides = overrides,
                        Registration = registration,
                        ContainerContext = Context,
                    };

                    return context.Pipeline(ref context);
                }));
            }

            // Existing pipeline
            var context = new BuilderContext
            {
                Async = true,

                List             = new PolicyList(),
                Type             = type,
                Overrides        = overrides,
                Registration     = registration,
                ContainerContext = Context,
            };

            // Execute the pipeline
            var task = context.Pipeline(ref context);

            // Make sure it is indeed a Task
            Debug.Assert(task is Task <object?>);

            // Return Task
            return((Task <object?>)task);
        }
コード例 #2
0
        ValueTask <object?> IUnityContainerAsync.ResolveAsync(Type type, string?name, params ResolverOverride[] overrides)
        {
            var registration = GetRegistration(type ?? throw new ArgumentNullException(nameof(type)), name);

            if (null != registration.LifetimeManager)
            {
                // Make unblocking check if already has result
                var value = registration.LifetimeManager.TryGet(LifetimeContainer);
                if (LifetimeManager.NoValue != value)
                {
                    return(new ValueTask <object?>(value));
                }
            }


            return(new ValueTask <object?>(Task.Factory.StartNew <object?>(delegate
            {
                // Check if already created and acquire a lock
                if (null != registration.LifetimeManager)
                {
                    var value = registration.LifetimeManager.Get(LifetimeContainer);
                    if (LifetimeManager.NoValue != value)
                    {
                        return new ValueTask <object?>(value);
                    }
                }

                // Setup Context
                var synchronized = registration.LifetimeManager as SynchronizedLifetimeManager;
                var context = new BuilderContext
                {
                    List = new PolicyList(),
                    Type = type,
                    Name = name,
                    Overrides = overrides,
                    Registration = registration,
                    ContainerContext = Context,
                };

                // Execute pipeline
                try
                {
                    var value = context.Pipeline(ref context);
                    registration.LifetimeManager?.Set(value, LifetimeContainer);
                    return value;
                }
                catch (Exception ex)
                {
                    synchronized?.Recover();

                    if (ex is InvalidRegistrationException ||
                        ex is CircularDependencyException ||
                        ex is ObjectDisposedException)
                    {
                        var message = CreateErrorMessage(ex);
                        throw new ResolutionFailedException(context.Type, context.Name, message, ex);
                    }
                    else
                    {
                        throw;
                    }
                }
            })));
        }
コード例 #3
0
        /// <inheritdoc />
        Task <object?> IUnityContainerAsync.Resolve(Type type, string?name, params ResolverOverride[] overrides)
        {
            // Setup Context
            var registration = GetRegistration(type ?? throw new ArgumentNullException(nameof(type)), name);

            // Check if pipeline exists
            if (null == registration.Pipeline)
            {
                // Start a new Task to create and execute pipeline
                return(Task.Factory.StartNew(() =>
                {
                    var context = new BuilderContext
                    {
                        List = new PolicyList(),
                        Type = type,
                        Overrides = overrides,
                        Registration = registration,
                        ContainerContext = Context,
                    };

                    try
                    {
                        // Execute pipeline
                        return context.Pipeline(ref context);
                    }
                    catch (Exception ex)
                        when(ex is InvalidRegistrationException ||
                             ex is CircularDependencyException ||
                             ex is ObjectDisposedException)
                        {
                            var message = CreateMessage(ex);
                            throw new ResolutionFailedException(context.Type, context.Name, message, ex);
                        }
                }));
            }

            // Existing pipeline
            var context = new BuilderContext
            {
                Async = true,

                List             = new PolicyList(),
                Type             = type,
                Overrides        = overrides,
                Registration     = registration,
                ContainerContext = Context,
            };

            try
            {
                // Execute pipeline
                var task = context.Pipeline(ref context);

                // Make sure it is indeed a Task
                Debug.Assert(task is Task <object?>);

                // Return Task
                return((Task <object?>)task);
            }
            catch (Exception ex)
                when(ex is InvalidRegistrationException || ex is CircularDependencyException)
                {
                    var message = CreateMessage(ex);

                    throw new ResolutionFailedException(context.Type, context.Name, message, ex);
                }
        }