Bagikan melalui


Interoperability with Enterprise Services and COM+ Transactions 

The System.Transactions namespace supports interoperability between transaction objects created using this namespace and transactions created through COM+.

You can use the EnterpriseServicesInteropOption enumeration when you create a new TransactionScope instance to specify the level of interoperability with COM+.

By default, when your application code checks the static Current property, System.Transactions attempts to look for a transaction that is otherwise current, or a TransactionScope object that dictates that Current is null. If it cannot find either one of these, System.Transactions queries the COM+ context for a transaction. Note that even though System.Transactions may find a transaction from the COM+ context, it still favors transactions that are native to System.Transactions.

Interoperability levels

The EnterpriseServicesInteropOption enumeration defines the following levels of interoperability—None, Full and Automatic.

The TransactionScope class provides constructors that accept EnterpriseServicesInteropOption as a parameter.

None, as the name implies, implies that there is no interoperability between System.EnterpriseServices contexts and transaction scopes. After creating a TransactionScope object with None, any changes to Current are not reflected in the COM+ context. Similarly, changes to the transaction in the COM+ context are not reflected in Current. This is the fastest mode of operation for System.Transactions because there is no extra synchronization required. None is the default value used by TransactionScope with all constructors that do not accept EnterpriseServicesInteropOption as a parameter.

If you do want to combine System.EnterpriseServices transactions with your ambient transaction, you need to use either Full or Automatic. Both of these values rely on a feature called services without components, and therefore you should be running on Windows XP Service Pack 2 or Windows Server 2003 when using them.

Full specifies that the ambient transactions for System.Transactions and System.EnterpriseServices are always the same. It results in creating a new System.EnterpriseServices transactional context and applying the transaction that is current for the TransactionScope to be current for that context. As such, the transaction in Current is completely in synchronization with the transaction in Transaction. This value introduces a performance penalty because new COM+ contexts may need to be created.

Automatic specifies the following requirements:

  • When Current is checked, System.Transactions should support transactions in the COM+ context if it detects that it is running in a context other than the default context. Note that the default context cannot contain a transaction. Therefore, in the default context, even with Automatic, the transaction stored in the thread local storage used by System.Transactions is returned for Current.

  • If a new TransactionScope object is created and the creation occurs in a context other than the default context, the transaction that is current for the TransactionScope object should be reflected in COM+. In this case, Automatic behaves like Full in that it creates a new COM+ context.

In addition when Current is set in both Full and Automatic, both of these modes imply that Current cannot be set directly. Any attempt to set Current directly other than creating a TransactionScope results in an InvalidOperationException. The EnterpriseServicesInteropOption enumeration value is inherited by new transaction scopes that do not explicitly specify which value to use. For example, if you create a new TransactionScope object with Full, and then create a second TransactionScope object but do not specify an EnterpriseServicesInteropOption value, the second TransactionScope object also has a Full.

In summary, the following rules apply when creating a new transaction scope:

  1. Current is checked to see if there is a transaction. This check results in:

    • A check to see if there is a scope.

    • If there is a scope, the value of the EnterpriseServicesInteropOption enumeration passed in when the scope was initially created is checked.

    • If the EnterpriseServicesInteropOption enumeration is set to Automatic, the COM+ transaction (System.EnterpriseServices Transaction) takes precedence over the System.Transactions transaction in managed thread local storage.

      If the value is set to None, the System.Transactions transaction in managed thread local storage takes precedence.

      If the value is Full, there is only one transaction and it is a COM+ transaction.

  2. The value of the TransactionScopeOption enumeration passed in by the TransactionScope constructor is checked. This determines if a new transaction must be created.

  3. If a new transaction is to be created, the following values of EnterpriseServicesInteropOption result in:

    • Full: a transaction associated with a COM+ context is created.

    • None: a System.Transactions transaction is created.

    • Automatic: if there is a COM+ context, a transaction is created and attached to the context.

The following table illustrates the Enterprise Services (ES) context and a transactional scope that requires a transaction using the EnterpriseServicesInteropOption enumeration.

ES Context None Automatic Full

Default context

Default context

Default context

Create new transactional context

Non-default context

Maintain client's context

Create new transactional context

Create new transactional context

The following table illustrates what the ambient transaction is, given a particular System.EnterpriseServices context, and a transactional scope that requires a transaction using the EnterpriseServicesInteropOption enumeration.

ES Context None Automatic Full

Default context

ST

ST

ES

Non-default context

ST

ES

ES

In the preceding table:

  • ST means that the scope's ambient transaction is managed by System.Transactions, separate from any System.EnterpriseServices context's transaction that may be present.

  • ES means that the scope's ambient transaction is same as the System.EnterpriseServices context's transaction.