internal ExecutionContextScopeManager(Container container) { Requires.IsNotNull(container, nameof(container)); this.Container = container; }
/// <summary> /// Gets the Execution Context <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.GetCurrentExecutionContextScope().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 GetCurrentExecutionContextScope(this Container container) { Requires.IsNotNull(container, nameof(container)); return(container.GetExecutionContextScopeManager().CurrentScope); }
/// <summary> /// Begins a new execution context scope for the given <paramref name="container"/>. /// Services, registered using the <see cref="ExecutionContextScopeLifestyle"/> are cached during the /// lifetime of that scope. The scope should be disposed explicitly. /// </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> /// <example> /// <code lang="cs"><![CDATA[ /// using (container.BeginExecutionContextScope()) /// { /// var handler container.GetInstance(rootType) as IRequestHandler; /// /// handler.Handle(request); /// } /// ]]></code> /// </example> public static Scope BeginExecutionContextScope(this Container container) { Requires.IsNotNull(container, "container"); return(container.GetExecutionContextScopeManager().BeginExecutionContextScope()); }