コード例 #1
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="ServiceResolveContext"/> of the
        ///     specified <paramref name="parentContext"/>.
        /// </summary>
        /// <remarks>The depth automatically increases.</remarks>
        /// <param name="parentContext">the parent context to take the data from</param>
        /// <param name="constructionMode"></param>
        /// <param name="serviceType">the type of the new service being resolved</param>
        /// <exception cref="ArgumentNullException">
        ///     thrown if the specified <paramref name="parentContext"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        ///     thrown if the specified <paramref name="serviceType"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        ///     thrown if the maximum service resolve depth was exceeded.
        /// </exception>
        public ServiceResolveContext(ServiceResolveContext parentContext, ServiceConstructionMode constructionMode,
                                     Type serviceType)
        {
            if (parentContext is null)
            {
                throw new ArgumentNullException(nameof(parentContext));
            }

            ParentType       = parentContext.ServiceType;
            Resolver         = parentContext.Resolver;
            Register         = parentContext.Register;
            ServiceType      = serviceType;
            ConstructionMode = GetServiceConstructionMode(constructionMode, parentContext.Resolver, parentContext);

            // increase resolve depth and check if it was exceeded
            if ((Depth = parentContext.Depth + 1) >= parentContext.Resolver.MaximumDepth)
            {
                throw new InvalidOperationException($"The maximum service resolve depth ({Depth}) was exceeded.");
            }

#if DEBUG
            TraceBuilder = parentContext.TraceBuilder;
#endif // DEBUG
        }
コード例 #2
0
        private static ServiceConstructionMode GetServiceConstructionMode(ServiceConstructionMode mode,
                                                                          IServiceResolver resolver, ServiceResolveContext parentContext = null)
        {
            // check if the mode is parent and the parent context is available
            if (mode == ServiceConstructionMode.Parent && parentContext != null)
            {
                return(parentContext.ConstructionMode);
            }

            // check if not the default mode is used
            if (mode != ServiceConstructionMode.Default)
            {
                // use selected mode
                return(mode);
            }

            // use default construction mode
            return(resolver.ServiceConstructionMode);
        }