Example #1
0
        /// <summary>
        /// Begins a new lifetime scope for the given <paramref name="container"/> on the current thread.
        /// Services, registered with
        /// <see cref="RegisterLifetimeScope{TService, TImplementation}(Container)">RegisterLifetimeScope</see> or
        /// using the <see cref="LifetimeScopeLifestyle"/> and are requested within the same thread as where the
        /// lifetime scope is created, are cached during the lifetime of that scope.
        /// The scope should be disposed explicitly when the scope ends.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <returns>A new <see cref="Scope"/> instance.</returns>
        /// <exception cref="ArgumentNullException">
        /// Thrown when the <paramref name="container"/> is a null reference.</exception>
        /// <exception cref="InvalidOperationException">Thrown when <see cref="EnableLifetimeScoping"/> has
        /// not been called previously.</exception>
        /// <example>
        /// <code lang="cs"><![CDATA[
        /// using (container.BeginLifetimeScope())
        /// {
        ///     var handler container.GetInstance(rootType) as IRequestHandler;
        ///
        ///     handler.Handle(request);
        /// }
        /// ]]></code>
        /// </example>
        public static Scope BeginLifetimeScope(this Container container)
        {
            Requires.IsNotNull(container, "container");

            return(container.GetLifetimeScopeManager().BeginLifetimeScope());
        }
Example #2
0
        /// <summary>
        /// Gets the <see cref="Scope"/> that is currently in scope or <b>null</b> when no
        /// <see cref="Scope"/> is currently in scope.
        /// </summary>
        /// <example>
        /// The following example registers a <b>ServiceImpl</b> type as transient (a new instance will be
        /// returned every time) and registers an initializer for that type that will register that instance
        /// for disposal in the <see cref="Scope"/> in which context it is created:
        /// <code lang="cs"><![CDATA[
        /// container.Register<IService, ServiceImpl>();
        /// container.RegisterInitializer<ServiceImpl>(instance =>
        /// {
        ///     container.GetCurrentLifetimeScope().RegisterForDisposal(instance);
        /// });
        /// ]]></code>
        /// </example>
        /// <param name="container">The container.</param>
        /// <returns>A new <see cref="Scope"/> instance.</returns>
        /// <exception cref="ArgumentNullException">
        /// Thrown when the <paramref name="container"/> is a null reference.</exception>
        public static Scope GetCurrentLifetimeScope(this Container container)
        {
            Requires.IsNotNull(container, "container");

            return(container.GetLifetimeScopeManager().CurrentScope);
        }
Example #3
0
 internal InjectionTargetInfo(ParameterInfo parameter)
 {
     Requires.IsNotNull(parameter, "parameter");
     this.Parameter = parameter;
 }
Example #4
0
 internal InjectionTargetInfo(PropertyInfo property)
 {
     Requires.IsNotNull(property, "property");
     this.Property = property;
 }
Example #5
0
 public static void EnableLifetimeScoping(this Container container)
 {
     // This method has become a no-op.
     Requires.IsNotNull(container, "container");
 }
Example #6
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));

            return(this.CreateRegistration(concreteType, concreteType, container));
        }
Example #7
0
        /// <summary>
        /// Get service of type <typeparamref name="T"/> from the list of request-specific services of the
        /// application builder. This preserves the lifestyle of the registered component.
        /// </summary>
        /// <typeparam name="T">The type of service object to get.</typeparam>
        /// <param name="builder">The IApplicationBuilder to retrieve the service object from.</param>
        /// <returns>A service object of type T or null if there is no such service.</returns>
        /// <exception cref="InvalidOperationException">Thrown when the method is called outside the
        /// context of a web request.</exception>
        public static T GetRequestService <T>(this IApplicationBuilder builder)
        {
            Requires.IsNotNull(builder, nameof(builder));

            return(GetRequestServiceProvider(builder.GetApplicationServices(), typeof(T)).GetService <T>());
        }
Example #8
0
        internal static string ToCSharpFriendlyName(Type genericTypeDefinition, bool fullyQualifiedName)
        {
            Requires.IsNotNull(genericTypeDefinition, nameof(genericTypeDefinition));

            return(genericTypeDefinition.ToFriendlyName(fullyQualifiedName, CSharpFriendlyNameArgumentFormatter));
        }
        /// <summary>
        /// Gets the <see cref="Scope"/> for the current WCF request or <b>null</b> when no
        /// <see cref="Scope"/> is currently in scope.
        /// </summary>
        /// <example>
        /// The following example registers a <b>ServiceImpl</b> type as transient (a new instance will be
        /// returned every time) and registers an initializer for that type that will register that instance
        /// for disposal in the <see cref="Scope"/> in which context it is created:
        /// <code lang="cs"><![CDATA[
        /// container.Register<IService, ServiceImpl>();
        /// container.RegisterInitializer<ServiceImpl>(instance =>
        /// {
        ///     container.GetCurrentWcfOperationScope().RegisterForDisposal(instance);
        /// });
        /// ]]></code>
        /// </example>
        /// <param name="container">The container.</param>
        /// <returns>A new <see cref="Scope"/> instance.</returns>
        /// <exception cref="ArgumentNullException">
        /// Thrown when the <paramref name="container"/> is a null reference.</exception>
        /// <exception cref="InvalidOperationException">Thrown when the current <paramref name="container"/>
        /// has both no <b>LifetimeScope</b> registrations.
        /// </exception>
        public static Scope GetCurrentWcfOperationScope(this Container container)
        {
            Requires.IsNotNull(container, "container");

            return(WcfOperationLifestyle.GetCurrentScopeCore());
        }
        /// <summary>
        /// Registers all concrete, non-generic, public and internal types in the given
        /// <paramref name="assembly"/> that implement the given <paramref name="openGenericServiceType"/>
        /// with the supplied <paramref name="lifestyle"/>.
        /// <see cref="TypesToRegisterOptions.IncludeDecorators">Decorators</see> and
        /// <see cref="TypesToRegisterOptions.IncludeGenericTypeDefinitions">generic type definitions</see>
        /// will be excluded from registration, while
        /// <see cref="TypesToRegisterOptions.IncludeComposites">composites</see> are included.
        /// </summary>
        /// <param name="openGenericServiceType">The definition of the open generic type.</param>
        /// <param name="assembly">An assembly that will be searched.</param>
        /// <param name="lifestyle">The lifestyle to register instances with.</param>
        /// <exception cref="ArgumentNullException">Thrown when one of the arguments contain a null
        /// reference (Nothing in VB).</exception>
        /// <exception cref="ArgumentException">Thrown when <paramref name="openGenericServiceType"/> is not
        /// an open generic type.</exception>
        /// <exception cref="InvalidOperationException">Thrown when the given
        /// <paramref name="assembly"/> contain multiple types that implement the same
        /// closed generic version of the given <paramref name="openGenericServiceType"/>.</exception>
        public void Register(Type openGenericServiceType, Assembly assembly, Lifestyle lifestyle)
        {
            Requires.IsNotNull(assembly, nameof(assembly));

            this.Register(openGenericServiceType, new[] { assembly }, lifestyle);
        }
Example #11
0
        /// <summary>Initializes a new instance of the <see cref="Scope"/> class.</summary>
        /// <param name="container">The container instance that the scope belongs to.</param>
        public Scope(Container container)
        {
            Requires.IsNotNull(container, nameof(container));

            this.Container = container;
        }
Example #12
0
        /// <summary>
        /// Creates a new <see cref="Registration"/> instance defining the creation of the
        /// specified <typeparamref name="TConcrete"/> with the caching as specified by this lifestyle.
        /// </summary>
        /// <typeparam name="TConcrete">The concrete type that will be registered.</typeparam>
        /// <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>
        protected internal override Registration CreateRegistrationCore <TConcrete>(Container container)
        {
            Requires.IsNotNull(container, nameof(container));

            return(new ScopedRegistration <TConcrete>(this, container));
        }
        internal ContainerCollectionRegistrator(Container container)
        {
            Requires.IsNotNull(container, nameof(container));

            this.container = container;
        }
 private static string BuildMessage(IList <DiagnosticResult> errors)
 {
     Requires.IsNotNull(errors, "errors");
     return(StringResources.DiagnosticWarningsReported(errors));
 }
Example #15
0
        /// <summary>
        /// Returns the current <see cref="Scope"/> for this lifestyle and the given
        /// <paramref name="container"/>, or null when this method is executed outside the context of a scope.
        /// </summary>
        /// <param name="container">The container instance that is related to the scope to return.</param>
        /// <returns>A <see cref="Scope"/> instance or null when there is no scope active in this context.</returns>
        public Scope GetCurrentScope(Container container)
        {
            Requires.IsNotNull(container, "container");

            return(this.GetCurrentScopeCore(container) ?? container.VerificationScope);
        }
        /// <summary>
        /// Returns the current <see cref="Scope"/> for this lifestyle and the given
        /// <paramref name="container"/>, or null when this method is executed outside the context of a scope.
        /// </summary>
        /// <param name="container">The container instance that is related to the scope to return.</param>
        /// <returns>A <see cref="Scope"/> instance or null when there is no scope active in this context.</returns>
        public Scope?GetCurrentScope(Container container)
        {
            Requires.IsNotNull(container, nameof(container));

            return(this.GetCurrentScopeInternal(container));
        }
Example #17
0
        /// <summary>
        /// Creates a new <see cref="Registration"/> instance defining the creation of the
        /// specified <typeparamref name="TImplementation"/> with the caching as specified by this lifestyle.
        /// </summary>
        /// <typeparam name="TService">The interface or base type that can be used to retrieve the instances.</typeparam>
        /// <typeparam name="TImplementation">The concrete type that will be registered.</typeparam>
        /// <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>
        protected override Registration CreateRegistrationCore <TService, TImplementation>(Container container)
        {
            Requires.IsNotNull(container, "container");

            return(new ScopedRegistration <TService, TImplementation>(this, container, this.disposeInstances));
        }
Example #18
0
        public Registration CreateRegistration(Type serviceType, Type implementationType, Container container)
        {
            Requires.IsNotNull(implementationType, nameof(implementationType));

            return(this.CreateRegistration(implementationType, container));
        }
 /// <summary>
 /// Allows the container to resolve instances with a Per Wcf Operation lifestyle for the given
 /// <paramref name="container"/>. This is
 /// enabled automatically when services get registered using one of the <b>RegisterPerWcfRequest</b>
 /// overloads or when the container passed onto the
 /// <see cref="SimpleInjectorServiceHostFactory.SetContainer"/> method.
 /// </summary>
 /// <param name="container">The container.</param>
 public static void EnablePerWcfOperationLifestyle(this Container container)
 {
     // This method has become a no-op.
     Requires.IsNotNull(container, "container");
 }