Exemple #1
0
        /// <summary>
        ///     Causes the container to instantiate the service with the given
        ///     <paramref name="serviceType">service type</paramref>. If the service type cannot be created, then an
        ///     exception will be thrown if the <see cref="IContainer.SuppressErrors" /> property
        ///     is set to false. Otherwise, it will simply return null.
        /// </summary>
        /// <param name="serviceName">The name of the service to instantiate.</param>
        /// <param name="serviceType">The service type to instantiate.</param>
        /// <param name="additionalArguments">The additional arguments that will be used to instantiate the service type.</param>
        /// <returns>
        ///     If successful, it will return a service instance that is compatible with the given type;
        ///     otherwise, it will just return a <c>null</c> value.
        /// </returns>
        public virtual object GetService(string serviceName, Type serviceType, params object[] additionalArguments)
        {
            IFactory factory = null;

            if (FactoryStorage != null)
            {
                factory = FactoryStorage.GetFactory(serviceName, serviceType, additionalArguments);
            }

            var serviceRequest = new ServiceRequest(serviceName, serviceType, additionalArguments, factory, this);
            var instance       = _getServiceBehavior.GetService(serviceRequest);

            if (SuppressErrors == false && instance == null && serviceName == null)
            {
                throw new ServiceNotFoundException(serviceType);
            }

            if (SuppressErrors == false && instance == null && serviceName != null)
            {
                throw new NamedServiceNotFoundException(serviceName, serviceType);
            }

            return(instance);
        }
        /// <summary>
        /// Causes the container to instantiate the service with the given
        /// <paramref name="serviceType">service type</paramref>. If the service type cannot be created, then an
        /// exception will be thrown if the <see cref="IContainer.SuppressErrors"/> property
        /// is set to false. Otherwise, it will simply return null.
        /// </summary>
        /// <param name="serviceName">The name of the service to instantiate.</param>
        /// <param name="serviceType">The service type to instantiate.</param>        
        /// <param name="additionalArguments">The additional arguments that will be used to instantiate the service type.</param>
        /// <returns>If successful, it will return a service instance that is compatible with the given type;
        /// otherwise, it will just return a <c>null</c> value.</returns>
        public virtual object GetService(string serviceName, Type serviceType, params object[] additionalArguments)
        {
            IFactory factory = null;

            if (FactoryStorage != null)
                factory = FactoryStorage.GetFactory(serviceName, serviceType, additionalArguments);

            var serviceRequest = new ServiceRequest(serviceName, serviceType, additionalArguments, factory, this);
            var instance = _getServiceBehavior.GetService(serviceRequest);

            if (SuppressErrors == false && instance == null && serviceName == null)
                throw new ServiceNotFoundException(serviceType);

            if (SuppressErrors == false && instance == null && serviceName != null)
                throw new NamedServiceNotFoundException(serviceName, serviceType);

            return instance;
        }