src/Autofac/Core/Lifetime/LifetimeScope.cs
/// <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>
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.
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 .
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);
            }
            BuildCallbackManager.RunBuildCallbacks(scope);
            RaiseBeginning(scope);
            return scope;
        }
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 ) ; } BuildCallbackManager . RunBuildCallbacks ( scope ) ; RaiseBeginning ( scope ) ; return scope ; }
Begin a new tagged sub-scope, with additional components available to it.
Begin a new tagged sub - scope with additional components available to it .
// Run any build callbacks.
A new lifetime scope.
A new lifetime scope .
tag
object
The tag applied to the .
The tag applied to the .
configurationAction
Action<ContainerBuilder>
Action on a
that adds component registrations visible only in the new scope.
Action on a that adds component registrations visible only in the new scope .
example
IContainer cr =
using (var lifetime = cr.BeginLifetimeScope("unitOfWork", builder => {
builder.RegisterType();
builder.RegisterType().As(); })
{
var foo = lifetime.Resolve();
}
IContainer cr = using ( var lifetime = cr . BeginLifetimeScope ( " unitOfWork " builder = > { builder . RegisterType () ; builder . RegisterType () . As () ; } ) { var foo = lifetime . Resolve () ; }