Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TransactionScope across AppDomains and processes

Is it real to use System.Transactions (primarily TransactionScope) across different AppDomains and processes?

DependentTransaction works only inside one AppDomain.

like image 333
SiberianGuy Avatar asked Mar 18 '11 04:03

SiberianGuy


People also ask

What is TransactionScope?

The TransactionScope class provides a simple way to mark a block of code as participating in a transaction, without requiring you to interact with the transaction itself. A transaction scope can select and manage the ambient transaction automatically.

What is an ambient transaction?

An ambient transaction is one that works at the thread level. Thus, all operations that occur in that context will be part of the transaction.

What is transaction scope in SQL Server?

The TransactionScope class makes a code block transactional by implicitly enlisting connections in a distributed transaction. You must call the Complete method at the end of the TransactionScope block before leaving it. Leaving the block invokes the Dispose method.


1 Answers

Yes, it works. We are flowing transactions via WCF, calling out of process transactional COM+ components, and manually passing transactions from a .NET 2.0 asmx web service to a WCF service.

Now that is not to say that the setup is not finicky. I think most of the issues were around getting MSDTC set up properly on all the servers.

UPDATE

We don't use DependentClone. We are passing the transaction as a byte array using GetTransactionFromTransmitterPropagationToken. Very similar to the second example of Propagating a Transaction Across AppDomains.

As an example:

Client:

public void CallOutOfProcessAndPassTransaction
{
    Client client = new Client();

    client.DoSomethingTransactional(
        System.Transactions.TransactionInterop.GetTransmitterPropagationToken(
            System.Transactions.Transaction.Current)
    );
}

Service:

public void DoSomethingTransactional(byte[] tx)
{
    using (TransactionScope ts = new TransactionScope(
               TransactionInterop.GetTransactionFromTransmitterPropagationToken(tx)))
    {
        // Do Something

        // vote to commit the transaction if the caller also agrees
        ts.Complete();
    }
}
like image 147
Randy supports Monica Avatar answered Nov 07 '22 13:11

Randy supports Monica