/// <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 => { /// builder.RegisterType<Foo>(); /// builder.RegisterType<Bar>().As<IBar>(); }) /// { /// var foo = lifetime.Resolve<Foo>(); /// } /// </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); }
public void Update(IContainer container, ContainerBuildOptions options) { // Issue #462: The ContainerBuildOptions parameter is added here as an overload // rather than an optional parameter to avoid method binding issues. In version // 4.0 or later we should refactor this to be an optional parameter. if (container == null) { throw new ArgumentNullException(nameof(container)); } Update(container.ComponentRegistry); if ((options & ContainerBuildOptions.IgnoreStartableComponents) == ContainerBuildOptions.None) { StartableManager.StartStartableComponents(container); } }
/// <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) { var result = new Container(Properties); result.ComponentRegistry.Properties[MetadataKeys.ContainerBuildOptions] = options; Build(result.ComponentRegistry, (options & ContainerBuildOptions.ExcludeDefaultModules) != ContainerBuildOptions.None); if ((options & ContainerBuildOptions.IgnoreStartableComponents) == ContainerBuildOptions.None) { StartableManager.StartStartableComponents(result); } var buildCallbacks = GetBuildCallbacks(); foreach (var buildCallback in buildCallbacks) { buildCallback(result); } return(result); }
/// <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); }