Example #1
0
        public void SingletonsFromRegistrationSourceAreWrappedWithLifetimeDecorator()
        {
            var restrictedRootScopeLifetime = new MatchingScopeLifetime(new object());
            var tracker = new ScopeRestrictedRegisteredServicesTracker(restrictedRootScopeLifetime);

            var builder = new ComponentRegistryBuilder(tracker, new Dictionary <string, object>());

            builder.AddRegistrationSource(new ObjectRegistrationSource());

            var typedService = new TypedService(typeof(object));
            var registry     = builder.Build();

            registry.TryGetRegistration(typedService, out IComponentRegistration registration);

            Assert.IsType <ComponentRegistrationLifetimeDecorator>(registration);
        }
Example #2
0
        /// <summary>
        /// Creates and setup the registry for a child scope.
        /// </summary>
        /// <param name="tag">The tag applied to the <see cref="ILifetimeScope"/>.</param>
        /// <param name="configurationAction">Action on a <see cref="ContainerBuilder"/>
        /// that adds component registrations visible only in the child scope.</param>
        /// <returns>Registry to use for a child scope.</returns>
        /// <remarks>It is the responsibility of the caller to make sure that the registry is properly
        /// disposed of. This is generally done by adding the registry to the <see cref="Disposer"/>
        /// property of the child scope.</remarks>
        private IComponentRegistryBuilder CreateScopeRestrictedRegistry(object tag, Action <ContainerBuilder> configurationAction)
        {
            var restrictedRootScopeLifetime = new MatchingScopeLifetime(tag);
            var tracker = new ScopeRestrictedRegisteredServicesTracker(restrictedRootScopeLifetime);

            var fallbackProperties = new FallbackDictionary <string, object?>(ComponentRegistry.Properties);
            var registryBuilder    = new ComponentRegistryBuilder(tracker, fallbackProperties);

            var builder = new ContainerBuilder(fallbackProperties, registryBuilder);

            foreach (var source in ComponentRegistry.Sources)
            {
                if (source.IsAdapterForIndividualComponents)
                {
                    builder.RegisterSource(source);
                }
            }

            // Issue #272: Only the most nested parent registry with HasLocalComponents is registered as an external source
            // It provides all non-adapting registrations from itself and from it's parent registries
            ISharingLifetimeScope?parent = this;

            while (parent != null)
            {
                if (parent.ComponentRegistry.HasLocalComponents)
                {
                    var externalSource = new ExternalRegistrySource(parent.ComponentRegistry);
                    builder.RegisterSource(externalSource);
                    break;
                }

                parent = parent.ParentLifetimeScope;
            }

            configurationAction(builder);

            builder.UpdateRegistry(registryBuilder);
            return(registryBuilder);
        }
Example #3
0
        /// <summary>
        /// Create a new container with the component registrations that have been made.
        /// </summary>
        /// <param name="options">Options that influence the way the container is initialised.</param>
        /// <remarks>
        /// Build can only be called once per <see cref="ContainerBuilder"/>
        /// - this prevents ownership issues for provided instances.
        /// Build enables support for the relationship types that come with Autofac (e.g.
        /// Func, Owned, Meta, Lazy, IEnumerable.) To exclude support for these types,
        /// first create the container, then call Update() on the builder.
        /// </remarks>
        /// <returns>A new container with the configured component registrations.</returns>
        public IContainer Build(ContainerBuildOptions options = ContainerBuildOptions.None)
        {
            Properties[MetadataKeys.ContainerBuildOptions] = options;

#pragma warning disable CA2000 // Dispose objects before losing scope
            ComponentRegistryBuilder.Register(new SelfComponentRegistration());
#pragma warning restore CA2000 // Dispose objects before losing scope

            Build(ComponentRegistryBuilder, (options & ContainerBuildOptions.ExcludeDefaultModules) != ContainerBuildOptions.None);

            var componentRegistry = ComponentRegistryBuilder.Build();

            var result = new Container(componentRegistry);
            if ((options & ContainerBuildOptions.IgnoreStartableComponents) == ContainerBuildOptions.None)
            {
                StartableManager.StartStartableComponents(Properties, result);
            }

            // Run any build callbacks.
            BuildCallbackManager.RunBuildCallbacks(result);

            return(result);
        }