示例#1
0
        /// <summary>
        /// Creates a new <see cref="Registration"/> instance defining the creation of the
        /// specified <paramref name="concreteType"/> with the caching as specified by this lifestyle.
        /// This method might fail when run in a partial trust sandbox when <paramref name="concreteType"/>
        /// is an internal type.
        /// </summary>
        /// <param name="concreteType">The concrete type that will be registered.</param>
        /// <param name="container">The <see cref="Container"/> instance for which a
        /// <see cref="Registration"/> must be created.</param>
        /// <returns>A new <see cref="Registration"/> instance.</returns>
        /// <exception cref="ArgumentNullException">Thrown when on of the supplied arguments is a null
        /// reference (Nothing in VB).</exception>
        public Registration CreateRegistration(Type concreteType, Container container)
        {
            Requires.IsNotNull(concreteType, nameof(concreteType));
            Requires.IsNotOpenGenericType(concreteType, nameof(concreteType));

            return(this.CreateRegistration(concreteType, concreteType, container));
        }
示例#2
0
        /// <summary>
        /// Creates a new <see cref="Registration"/> instance defining the creation of the
        /// specified <paramref name="serviceType"/>  using the supplied <paramref name="instanceCreator"/>
        /// with the caching as specified by this lifestyle.
        /// </summary>
        /// <param name="serviceType">The interface or base type that can be used to retrieve the instances.</param>
        /// <param name="instanceCreator">The delegate that will be responsible for creating new instances.</param>
        /// <param name="container">The <see cref="Container"/> instance for which a
        /// <see cref="Registration"/> must be created.</param>
        /// <returns>A new <see cref="Registration"/> instance.</returns>
        /// <exception cref="ArgumentNullException">Thrown when on of the supplied arguments is a null
        /// reference (Nothing in VB).</exception>
        public Registration CreateRegistration(Type serviceType, Func <object> instanceCreator,
                                               Container container)
        {
            Requires.IsNotNull(serviceType, nameof(serviceType));
            Requires.IsNotNull(instanceCreator, nameof(instanceCreator));
            Requires.IsNotNull(container, nameof(container));

            Requires.IsReferenceType(serviceType, nameof(serviceType));
            Requires.IsNotOpenGenericType(serviceType, nameof(serviceType));

            var closedCreateRegistrationMethod = OpenCreateRegistrationTServiceFuncMethod
                                                 .MakeGenericMethod(serviceType);

            try
            {
                // Build the following delegate: () => (ServiceType)instanceCreator();
                var typeSafeInstanceCreator = ConvertDelegateToTypeSafeDelegate(serviceType, instanceCreator);

                return((Registration)closedCreateRegistrationMethod.Invoke(this,
                                                                           new object[] { typeSafeInstanceCreator, container }));
            }
            catch (MemberAccessException ex)
            {
                throw BuildUnableToResolveTypeDueToSecurityConfigException(serviceType, ex, nameof(serviceType));
            }
        }
示例#3
0
        /// <summary>
        /// Creates a new <see cref="Registration"/> instance defining the creation of the
        /// specified <paramref name="implementationType"/> with the caching as specified by this lifestyle.
        /// This method might fail when run in a partial trust sandbox when <paramref name="implementationType"/>
        /// is an internal type.
        /// </summary>
        /// <param name="serviceType">The interface or base type that can be used to retrieve the instances.</param>
        /// <param name="implementationType">The concrete type that will be registered.</param>
        /// <param name="container">The <see cref="Container"/> instance for which a
        /// <see cref="Registration"/> must be created.</param>
        /// <returns>A new <see cref="Registration"/> instance.</returns>
        /// <exception cref="ArgumentNullException">Thrown when on of the supplied arguments is a null
        /// reference (Nothing in VB).</exception>
        public Registration CreateRegistration(Type serviceType, Type implementationType, Container container)
        {
            Requires.IsNotNull(serviceType, nameof(serviceType));
            Requires.IsNotNull(implementationType, nameof(implementationType));
            Requires.IsNotNull(container, nameof(container));

            Requires.IsReferenceType(serviceType, nameof(serviceType));
            Requires.IsReferenceType(implementationType, nameof(implementationType));

            Requires.IsNotOpenGenericType(serviceType, nameof(serviceType));
            Requires.IsNotOpenGenericType(implementationType, nameof(implementationType));

            Requires.ServiceIsAssignableFromImplementation(serviceType, implementationType,
                                                           nameof(implementationType));

            var closedCreateRegistrationMethod = OpenCreateRegistrationTServiceTImplementationMethod
                                                 .MakeGenericMethod(serviceType, implementationType);

            try
            {
                return((Registration)closedCreateRegistrationMethod.Invoke(this, new object[] { container }));
            }
            catch (MemberAccessException ex)
            {
                throw BuildUnableToResolveTypeDueToSecurityConfigException(implementationType, ex,
                                                                           nameof(implementationType));
            }
        }
示例#4
0
        public static void UseMiddleware(
            this SimpleInjectorUseOptions options, Type middlewareType, IApplicationBuilder app)
        {
            Requires.IsNotNull(options, nameof(options));
            Requires.IsNotNull(middlewareType, nameof(middlewareType));
            Requires.IsNotNull(app, nameof(app));

            Requires.ServiceIsAssignableFromImplementation(
                typeof(IMiddleware), middlewareType, nameof(middlewareType));
            Requires.IsNotOpenGenericType(middlewareType, nameof(middlewareType));

            var container = options.Container;

            var lifestyle = container.Options.LifestyleSelectionBehavior.SelectLifestyle(middlewareType);

            // By creating an InstanceProducer up front, it will be known to the container, and will be part
            // of the verification process of the container.
            InstanceProducer <IMiddleware> producer =
                lifestyle.CreateProducer <IMiddleware>(middlewareType, container);

            app.Use((c, next) =>
            {
                IMiddleware middleware = producer.GetInstance();
                return(middleware.InvokeAsync(c, _ => next()));
            });
        }
示例#5
0
        /// <summary>
        /// Adds a middleware type to the application's request pipeline. The middleware will be resolved from
        /// the supplied the Simple Injector <paramref name="container"/>. The middleware will be added to the
        /// container for verification.
        /// </summary>
        /// <param name="app">The <see cref="IApplicationBuilder"/> instance.</param>
        /// <param name="middlewareType">The middleware type that needs to be applied. This type must
        /// implement <see cref="IMiddleware"/>.</param>
        /// <param name="container">The container to resolve <paramref name="middlewareType"/> from.</param>
        /// <returns>The supplied <see cref="IApplicationBuilder"/> instance.</returns>
        /// <exception cref="ArgumentNullException">Thrown when one of the arguments is a null reference.</exception>
        /// <exception cref="ArgumentException">Thrown when the <paramref name="middlewareType"/> does not
        /// derive from <see cref="IMiddleware"/>, is an open-generic type, or not a concrete constructable
        /// type.</exception>
        public static IApplicationBuilder UseMiddleware(
            this IApplicationBuilder app, Type middlewareType, Container container)
        {
            Requires.IsNotNull(app, nameof(app));
            Requires.IsNotNull(middlewareType, nameof(middlewareType));
            Requires.IsNotNull(container, nameof(container));

            Requires.ServiceIsAssignableFromImplementation(
                typeof(IMiddleware), middlewareType, nameof(middlewareType));

            Requires.IsNotOpenGenericType(middlewareType, nameof(middlewareType));

            var lifestyle = container.Options.LifestyleSelectionBehavior.SelectLifestyle(middlewareType);

            // By creating an InstanceProducer up front, it will be known to the container, and will be part
            // of the verification process of the container.
            // Note that the middleware can't be registered in the container, because at this point the
            // container might already be locked (which will happen when the new ASP.NET Core 3 Host class is
            // used).
            InstanceProducer <IMiddleware> producer =
                lifestyle.CreateProducer <IMiddleware>(middlewareType, container);

            app.Use((c, next) =>
            {
                IMiddleware middleware = producer.GetInstance();
                return(middleware.InvokeAsync(c, _ => next()));
            });

            return(app);
        }
示例#6
0
        /// <summary>
        /// Creates a new <see cref="InstanceProducer"/> instance for the given <paramref name="serviceType"/>
        /// that will create new instances of specified <paramref name="implementationType"/> with the
        /// caching as specified by this lifestyle.
        /// </summary>
        /// <param name="serviceType">The interface or base type that can be used to retrieve the instances.</param>
        /// <param name="implementationType">The concrete type that will be registered.</param>
        /// <param name="container">The <see cref="Container"/> instance for which a
        /// <see cref="Registration"/> must be created.</param>
        /// <returns>A new <see cref="InstanceProducer"/> instance.</returns>
        /// <exception cref="ArgumentNullException">Thrown when on of the supplied arguments is a null
        /// reference (Nothing in VB).</exception>
        public InstanceProducer CreateProducer(Type serviceType, Type implementationType, Container container)
        {
            Requires.IsNotNull(serviceType, nameof(serviceType));
            Requires.IsNotNull(implementationType, nameof(implementationType));
            Requires.IsNotNull(container, nameof(container));
            Requires.IsNotOpenGenericType(implementationType, nameof(implementationType));

            return(new InstanceProducer(serviceType, this.CreateRegistration(implementationType, container)));
        }
示例#7
0
        /// <summary>
        /// Creates a new <see cref="Registration"/> instance defining the creation of the
        /// specified <paramref name="concreteType"/> with the caching as specified by this lifestyle,
        /// or returns an already created <see cref="Registration"/> instance for this container + lifestyle
        /// + type combination.
        /// This method might fail when run in a partial trust sandbox when <paramref name="concreteType"/>
        /// is an internal type.
        /// </summary>
        /// <param name="concreteType">The concrete type that will be registered.</param>
        /// <param name="container">The <see cref="Container"/> instance for which a
        /// <see cref="Registration"/> must be created.</param>
        /// <returns>A new <see cref="Registration"/> instance.</returns>
        /// <exception cref="ArgumentNullException">Thrown when on of the supplied arguments is a null
        /// reference (Nothing in VB).</exception>
        public Registration CreateRegistration(Type concreteType, Container container)
        {
            Requires.IsNotNull(concreteType, nameof(concreteType));
            Requires.IsNotNull(container, nameof(container));

            Requires.IsReferenceType(concreteType, nameof(concreteType));

            Requires.IsNotOpenGenericType(concreteType, nameof(concreteType));

            return(this.CreateRegistrationInternal(concreteType, container, preventTornLifestyles: true));
        }
示例#8
0
        /// <summary>
        /// Creates a new <see cref="InstanceProducer"/> instance for the given <typeparamref name="TService"/>
        /// that will create new instances of specified <paramref name="implementationType"/> caching as
        /// specified by this lifestyle.
        /// </summary>
        /// <typeparam name="TService">The interface or base type that can be used to retrieve the instances.</typeparam>
        /// <param name="implementationType">The concrete type that will be created.</param>
        /// <param name="container">The <see cref="Container"/> instance for which a
        /// <see cref="InstanceProducer"/> must be created.</param>
        /// <returns>A new <see cref="InstanceProducer"/> instance.</returns>
        /// <exception cref="ArgumentNullException">Thrown when either <paramref name="implementationType"/> or
        /// <paramref name="container"/> are null references (Nothing in VB).</exception>
        public InstanceProducer <TService> CreateProducer <TService>(Type implementationType, Container container)
            where TService : class
        {
            Requires.IsNotNull(implementationType, nameof(implementationType));
            Requires.IsNotNull(container, nameof(container));

            Requires.IsNotOpenGenericType(implementationType, nameof(implementationType));
            Requires.ServiceIsAssignableFromImplementation(typeof(TService), implementationType,
                                                           nameof(implementationType));

            return(new InstanceProducer <TService>(this.CreateRegistration(implementationType, container)));
        }
示例#9
0
        /// <summary>
        /// Conditionally registers that <paramref name="registration"/> will be used every time a
        /// <paramref name="serviceType"/> is requested and where the supplied <paramref name="predicate"/>
        /// returns true. The predicate will only be evaluated a finite number of times; the predicate is
        /// unsuited for making decisions based on runtime conditions.
        /// </summary>
        /// <param name="serviceType">The base type or interface to register. This can be an open-generic type.</param>
        /// <param name="registration">The <see cref="Registration"/> instance to register.</param>
        /// <param name="predicate">The predicate that determines whether the
        /// <paramref name="registration"/> can be applied for the requested service type. This predicate
        /// can be used to build a fallback mechanism where multiple registrations for the same service type
        /// are made. Note that the predicate will be called a finite number of times and its result will be cached
        /// for the lifetime of the container. It can't be used for selecting a type based on runtime conditions.
        /// </param>
        /// <exception cref="ArgumentNullException">Thrown when one of the arguments is a null reference
        /// (Nothing in VB).</exception>
        /// <exception cref="ArgumentException">Thrown when <paramref name="serviceType"/> is open generic or
        /// <paramref name="registration" /> is not assignable to <paramref name="serviceType"/>.</exception>
        /// <exception cref="InvalidOperationException">
        /// Thrown when this container instance is locked and can not be altered.
        /// </exception>
        public void RegisterConditional(Type serviceType, Registration registration,
                                        Predicate <PredicateContext> predicate)
        {
            Requires.IsNotNull(serviceType, nameof(serviceType));
            Requires.IsNotNull(registration, nameof(registration));
            Requires.IsNotNull(predicate, nameof(predicate));
            Requires.IsNotOpenGenericType(serviceType, nameof(serviceType));
            Requires.ServiceIsAssignableFromImplementation(serviceType, registration.ImplementationType,
                                                           nameof(serviceType));

            this.ThrowWhenContainerIsLockedOrDisposed();

            var producer = new InstanceProducer(serviceType, registration, predicate);

            this.AddInstanceProducer(producer);
        }
        internal InstanceProducer(Type serviceType, Registration registration, bool registerExternalProducer)
        {
            Requires.IsNotNull(serviceType, nameof(serviceType));
            Requires.IsNotNull(registration, nameof(registration));
            Requires.IsNotOpenGenericType(serviceType, nameof(serviceType));

            this.ServiceType  = serviceType;
            this.Registration = registration;
            this.validator    = new CyclicDependencyValidator(this);

            this.lazyExpression = new LazyEx <Expression>(this.BuildExpressionInternal);

            if (registerExternalProducer)
            {
                registration.Container.RegisterExternalProducer(this);
            }

            this.instanceCreator = this.BuildAndReplaceInstanceCreatorAndCreateFirstInstance;
        }
示例#11
0
        /// <summary>
        /// Registers a dynamic (container uncontrolled) collection of elements of type
        /// <paramref name="serviceType"/>. A call to <see cref="GetAllInstances{T}"/> will return the
        /// <paramref name="containerUncontrolledCollection"/> itself, and updates to the collection will be
        /// reflected in the result. If updates are allowed, make sure the collection can be iterated safely
        /// if you're running a multi-threaded application.
        /// </summary>
        /// <param name="serviceType">The base type or interface for elements in the collection.</param>
        /// <param name="containerUncontrolledCollection">The collection of items to register.</param>
        /// <exception cref="ArgumentNullException">Thrown when one of the supplied arguments is a null
        /// reference (Nothing in VB).</exception>
        /// <exception cref="ArgumentException">Thrown when <paramref name="serviceType"/> represents an
        /// open generic type.</exception>
        public void RegisterCollection(Type serviceType, IEnumerable containerUncontrolledCollection)
        {
            Requires.IsNotNull(serviceType, nameof(serviceType));
            Requires.IsNotNull(containerUncontrolledCollection, nameof(containerUncontrolledCollection));
            Requires.IsNotOpenGenericType(serviceType, nameof(serviceType));
            Requires.IsNotAnAmbiguousType(serviceType, nameof(serviceType));

            try
            {
                this.RegisterContainerUncontrolledCollection(serviceType,
                                                             containerUncontrolledCollection.Cast <object>());
            }
            catch (MemberAccessException ex)
            {
                // This happens when the user tries to resolve an internal type inside a (Silverlight) sandbox.
                throw new ArgumentException(
                          StringResources.UnableToResolveTypeDueToSecurityConfiguration(serviceType, ex) +
                          Environment.NewLine + "paramName: " + nameof(serviceType), ex);
            }
        }