/// <summary> /// Associates a <see cref="ClientTransaction"/> with the current thread, specifying the scope's automatic rollback behavior. /// </summary> /// <param name="scopedCurrentTransaction">The <see cref="ClientTransaction"/> object used as the current transaction until the scope is left.</param> /// <param name="autoRollbackBehavior">The automatic rollback behavior to be exhibited by this scope.</param> /// <param name="attachedScope">A second scope to be closed when this scope is left.</param> /// <remarks> /// <para> /// The <see cref="ClientTransactionScope"/> constructor stores the previous <see cref="ClientTransactionScope.ActiveScope"/>. When this scope's /// <see cref="Leave"/> method is called or the scope is disposed of, the previous scope is reactivated. /// </para> /// </remarks> internal ClientTransactionScope(ClientTransaction scopedCurrentTransaction, AutoRollbackBehavior autoRollbackBehavior, IDisposable attachedScope) { _autoRollbackBehavior = autoRollbackBehavior; _previousScope = ClientTransactionScope.ActiveScope; ClientTransactionScope.SetActiveScope(this); _scopedTransaction = scopedCurrentTransaction; _attachedScope = attachedScope; }
/// <summary> /// Resets <see cref="CurrentTransaction"/> to the value it had before this scope was instantiated and performs the /// <see cref="AutoRollbackBehavior"/>. This method is ignored when executed more than once. /// </summary> public void Leave() { if (_hasBeenLeft) { throw new InvalidOperationException("The ClientTransactionScope has already been left."); } if (ActiveScope != this) { throw new InvalidOperationException("This ClientTransactionScope is not the active scope. Leave the active scope before leaving this one."); } ExecuteAutoRollbackBehavior(); if (_attachedScope != null) { _attachedScope.Dispose(); } ClientTransactionScope.SetActiveScope(_previousScope); _hasBeenLeft = true; }
private static void SetActiveScope(ClientTransactionScope scope) { s_scopeSingleton.SetCurrent(scope); }