Beispiel #1
0
        /// <summary>
        /// Begin a new tagged sub-scope, with additional components available to it.
        /// Component instances created via the new scope
        /// will be disposed along with it.
        /// </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 new scope.</param>
        /// <returns>A new lifetime scope.</returns>
        /// <example>
        /// <code>
        /// IContainer cr = // ...
        /// using (var lifetime = cr.BeginLifetimeScope("unitOfWork", builder =&gt; {
        ///         builder.RegisterType&lt;Foo&gt;();
        ///         builder.RegisterType&lt;Bar&gt;().As&lt;IBar&gt;(); })
        /// {
        ///     var foo = lifetime.Resolve&lt;Foo&gt;();
        /// }
        /// </code>
        /// </example>
        public ILifetimeScope BeginLifetimeScope(object tag, Action <ContainerBuilder> configurationAction)
        {
            if (configurationAction == null)
            {
                throw new ArgumentNullException(nameof(configurationAction));
            }

            CheckNotDisposed();
            CheckTagIsUnique(tag);

            var localsBuilder = CreateScopeRestrictedRegistry(tag, configurationAction);
            var scope         = new LifetimeScope(localsBuilder.Build(), this, tag);

            scope.Disposer.AddInstanceForDisposal(localsBuilder);

            if (localsBuilder.Properties.TryGetValue(MetadataKeys.ContainerBuildOptions, out var options) &&
                options != null &&
                !((ContainerBuildOptions)options).HasFlag(ContainerBuildOptions.IgnoreStartableComponents))
            {
                StartableManager.StartStartableComponents(localsBuilder.Properties, scope);
            }

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

            RaiseBeginning(scope);

            return(scope);
        }
Beispiel #2
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);
        }