System.Transactions.CallContextCurrentData.ClearCurrentData C# (CSharp) Method

ClearCurrentData() public static method

public static ClearCurrentData ( ContextKey contextKey, bool removeContextData ) : void
contextKey ContextKey
removeContextData bool
return void
        public static void ClearCurrentData(ContextKey contextKey, bool removeContextData)
        {
            // Get the current ambient CallContext.
            ContextKey key = s_currentTransaction.Value;
            if (contextKey != null || key != null)
            {
                // removeContextData flag is used for perf optimization to avoid removing from the table in certain nested TransactionScope usage. 
                if (removeContextData)
                {
                    // if context key is passed in remove that from the contextDataTable, otherwise remove the default context key. 
                    s_contextDataTable.Remove(contextKey ?? key);
                }

                if (key != null)
                {
                    s_currentTransaction.Value = null;
                }
            }
        }

Usage Example

Example #1
0
        // PushScope
        //
        // Push a transaction scope onto the stack.
        private void PushScope()
        {
            // Fixup the interop mode before we set current.
            if (!_interopModeSpecified)
            {
                // Transaction.InteropMode will take the interop mode on
                // for the scope in currentScope into account.
                _interopOption = Transaction.InteropMode(_savedCurrentScope);
            }

            // async function yield at await points and main thread can continue execution. We need to make sure the TLS data are restored appropriately.
            SaveTLSContextData();

            if (AsyncFlowEnabled)
            {
                // Async Flow is enabled and CallContext will be used for ambient transaction.
                _threadContextData = CallContextCurrentData.CreateOrGetCurrentData(ContextKey);

                if (_savedCurrentScope == null && _savedCurrent == null)
                {
                    // Clear TLS data so that transaction doesn't leak from current thread.
                    ContextData.TLSCurrentData = null;
                }
            }
            else
            {
                // Legacy TransactionScope. Use TLS to track ambient transaction context.
                _threadContextData = ContextData.TLSCurrentData;
                CallContextCurrentData.ClearCurrentData(ContextKey, false);
            }

            // This call needs to be done first
            SetCurrent(_expectedCurrent);
            _threadContextData.CurrentScope = this;
        }
All Usage Examples Of System.Transactions.CallContextCurrentData::ClearCurrentData