TransactionScope Class

 

Makes a code block transactional. This class cannot be inherited.

Namespace:   System.Transactions
Assembly:  System.Transactions (in System.Transactions.dll)

System.Object
  System.Transactions.TransactionScope

[<Sealed>]
type TransactionScope = 
    class
        interface IDisposable
    end

NameDescription
System_CAPS_pubmethodTransactionScope()

Initializes a new instance of the TransactionScope class.

System_CAPS_pubmethodTransactionScope(Transaction)

Initializes a new instance of the TransactionScope class and sets the specified transaction as the ambient transaction, so that transactional work done inside the scope uses this transaction.

System_CAPS_pubmethodTransactionScope(Transaction, TimeSpan)

Initializes a new instance of the TransactionScope class with the specified timeout value, and sets the specified transaction as the ambient transaction, so that transactional work done inside the scope uses this transaction.

System_CAPS_pubmethodTransactionScope(Transaction, TimeSpan, EnterpriseServicesInteropOption)

Initializes a new instance of the TransactionScope class with the specified timeout value and COM+ interoperability requirements, and sets the specified transaction as the ambient transaction, so that transactional work done inside the scope uses this transaction.

System_CAPS_pubmethodTransactionScope(Transaction, TimeSpan, TransactionScopeAsyncFlowOption)

[Supported in the .NET Framework 4.5.1 and later versions]

Initializes a new instance of the TransactionScope class with the specified timeout value, and sets the specified transaction as the ambient transaction, so that transactional work done inside the scope uses this transaction.

System_CAPS_pubmethodTransactionScope(Transaction, TransactionScopeAsyncFlowOption)

[Supported in the .NET Framework 4.5.1 and later versions]

Initializes a new instance of the TransactionScope class and sets the specified transaction as the ambient transaction, so that transactional work done inside the scope uses this transaction.

System_CAPS_pubmethodTransactionScope(TransactionScopeAsyncFlowOption)

[Supported in the .NET Framework 4.5.1 and later versions]

Initializes a new instance of the TransactionScope class with the specified asynchronous flow option.

System_CAPS_pubmethodTransactionScope(TransactionScopeOption)

Initializes a new instance of the TransactionScope class with the specified requirements.

System_CAPS_pubmethodTransactionScope(TransactionScopeOption, TimeSpan)

Initializes a new instance of the TransactionScope class with the specified timeout value and requirements.

System_CAPS_pubmethodTransactionScope(TransactionScopeOption, TimeSpan, TransactionScopeAsyncFlowOption)

[Supported in the .NET Framework 4.5.1 and later versions]

Initializes a new instance of the TransactionScope class with the specified timeout value, requirements, and asynchronous flow option.

System_CAPS_pubmethodTransactionScope(TransactionScopeOption, TransactionOptions)

Initializes a new instance of the TransactionScope class with the specified requirements.

System_CAPS_pubmethodTransactionScope(TransactionScopeOption, TransactionOptions, EnterpriseServicesInteropOption)

Initializes a new instance of the TransactionScope class with the specified scope and COM+ interoperability requirements, and transaction options.

System_CAPS_pubmethodTransactionScope(TransactionScopeOption, TransactionOptions, TransactionScopeAsyncFlowOption)

[Supported in the .NET Framework 4.5.1 and later versions]

Initializes a new instance of the TransactionScope class with the specified requirements and asynchronous flow option.

System_CAPS_pubmethodTransactionScope(TransactionScopeOption, TransactionScopeAsyncFlowOption)

[Supported in the .NET Framework 4.5.1 and later versions]

Initializes a new instance of the TransactionScope class with the specified requirements and asynchronous flow option.

NameDescription
System_CAPS_pubmethodComplete()

Indicates that all operations within the scope are completed successfully.

System_CAPS_pubmethodDispose()

Ends the transaction scope.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

The System.Transactions infrastructure provides both an explicit programming model based on the Transaction class, as well as an implicit programming model using the TransactionScope class, in which transactions are automatically managed by the infrastructure.

System_CAPS_importantImportant

It is recommended that you create implicit transactions using the TransactionScope class, so that the ambient transaction context is automatically managed for you. You should also use the TransactionScope and DependentTransaction class for applications that require the use of the same transaction across multiple function calls or multiple thread calls. For more information on this model, see the Implementing An Implicit Transaction Using Transaction Scope topic. For more information on writing a transactional application, see Writing A Transactional Application.

Upon instantiating a TransactionScope by the new statement, the transaction manager determines which transaction to participate in. Once determined, the scope always participates in that transaction. The decision is based on two factors: whether an ambient transaction is present and the value of the TransactionScopeOption parameter in the constructor. The ambient transaction is the transaction your code executes in. You can obtain a reference to the ambient transaction by calling the static Current property of the Transaction class. For more information on how this parameter is used, please see the "Transaction Flow Management" section of the Implementing An Implicit Transaction Using Transaction Scope topic.

If no exception occurs within the transaction scope (that is, between the initialization of the TransactionScope object and the calling of its Dispose method), then the transaction in which the scope participates is allowed to proceed. If an exception does occur within the transaction scope, the transaction in which it participates will be rolled back.

When your application completes all work it wants to perform in a transaction, you should call the Complete method only once to inform that transaction manager that it is acceptable to commit the transaction. Failing to call this method aborts the transaction.

A call to the Dispose method marks the end of the transaction scope. Exceptions that occur after calling this method may not affect the transaction.

If you modify the value of Current inside a scope, an exception is thrown when Dispose is called. However, at the end of the scope, the previous value is restored. In addition, if you call Dispose on Current inside a transaction scope that created the transaction, the transaction aborts at the end of the scope.

The following example demonstrates how to use the TransactionScope class to define a block of code to participate in a transaction.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 2.0

This type is thread safe.

System.Transactions Namespace
Implementing An Implicit Transaction Using Transaction Scope

Return to top
Show: