public void Dispose()
        {
            if (disposed)
            {
                return;
            }

            disposed = true;

            if (parentScope != null)
            {
                parentScope.nested--;
            }

            if (nested > 0)
            {
                transaction.Rollback();
                throw new InvalidOperationException("TransactionScope nested incorrectly");
            }

            if (Transaction.CurrentInternal != transaction)
            {
                if (transaction != null)
                {
                    transaction.Rollback();
                }
                if (Transaction.CurrentInternal != null)
                {
                    Transaction.CurrentInternal.Rollback();
                }

                throw new InvalidOperationException("Transaction.Current has changed inside of the TransactionScope");
            }

            if (Transaction.CurrentInternal == oldTransaction && oldTransaction != null)
            {
                oldTransaction.Scope = parentScope;
            }

            Transaction.CurrentInternal = oldTransaction;

            if (transaction == null)
            {
                /* scope was not in a transaction, (Suppress) */
                return;
            }

            transaction.Scope = null;

            if (!IsComplete)
            {
                transaction.Rollback();
                return;
            }

            if (!isRoot)
            {
                /* Non-root scope has completed+ended */
                return;
            }

            transaction.CommitInternal();
        }
Exemple #2
0
        public void Dispose()
        {
            if (disposed)
            {
                return;
            }

            disposed = true;

            if (parentScope != null)
            {
                parentScope.nested--;
            }

            if (nested > 0)
            {
                transaction.Rollback();
                throw new InvalidOperationException("TransactionScope nested incorrectly");
            }

            if (Transaction.CurrentInternal != transaction && !asyncFlowEnabled)
            {
                if (transaction != null)
                {
                    transaction.Rollback();
                }
                if (Transaction.CurrentInternal != null)
                {
                    Transaction.CurrentInternal.Rollback();
                }

                throw new InvalidOperationException("Transaction.Current has changed inside of the TransactionScope");
            }

            if (scopeTimer != null)
            {
                scopeTimer.Dispose();
            }

            if (asyncFlowEnabled)
            {
                if (oldTransaction != null)
                {
                    oldTransaction.Scope = parentScope;
                }

                var variedTransaction = Transaction.CurrentInternal;

                if (transaction == null && variedTransaction == null)
                {
                    /* scope was not in a transaction, (Suppress) */
                    return;
                }

                variedTransaction.Scope     = parentScope;
                Transaction.CurrentInternal = oldTransaction;

                transaction.Scope = null;

                if (IsAborted)
                {
                    throw new TransactionAbortedException("Transaction has aborted");
                }
                else if (!IsComplete)
                {
                    transaction.Rollback();
                    variedTransaction.Rollback();
                    return;
                }

                if (!isRoot)
                {
                    /* Non-root scope has completed+ended */
                    return;
                }

                variedTransaction.CommitInternal();
                transaction.CommitInternal();
            }
            else
            {
                if (Transaction.CurrentInternal == oldTransaction && oldTransaction != null)
                {
                    oldTransaction.Scope = parentScope;
                }

                Transaction.CurrentInternal = oldTransaction;

                if (transaction == null)
                {
                    /* scope was not in a transaction, (Suppress) */
                    return;
                }

                if (IsAborted)
                {
                    transaction.Scope = null;
                    throw new TransactionAbortedException("Transaction has aborted");
                }
                else if (!IsComplete)
                {
                    transaction.Rollback();
                    return;
                }

                if (!isRoot)
                {
                    /* Non-root scope has completed+ended */
                    return;
                }

                transaction.CommitInternal();

                transaction.Scope = null;
            }
        }