Wednesday 11 July 2012

Current cannot be set directly when Com+ Interop is enabled.

Every now and then I found one obscure error which there is no way of finding in the internet anyone with the same problem. Or at least any english speaker.

When you google for it you just found five results, and usually they are in a mix of Russian, Korean, Hebrew... so you don't have a clue what is going on anyway.

This is the first post of a tag I will call "Russian Result", where I will share with you those errors I found and how I fixed them. Most of them are the typical errors you don't get when starting a new project, but those you found when you are maintaining an existing system.

Today we will cover the following error: "Current cannot be set directly when Com+ Interop is enabled."

 This error appeared when we make one of our webservices, running in a machine dedicated for them, to start a transaction that should be resued by the business logic layer which is accessed through COM+. The reason for that new transaction was to make two separate calls to the BLL share the same transaction.

In order to make it work we needed to change the way the BLL inititates the TransactionScope so it does it with Interop support, so the transaction can be enlisted in the Transaction shared by COM+.

After changing the appropriate methods everything went ok, until one day the error was thrown by another piece of code that used the Full Interop transactions.

The piece of code failing looked like this:


Transaction currentTran = Transaction.Current;
DependentTransaction depTran;
depTran = currentTran.DependentClone(DependentCloneOption.BlockCommitUntilComplete);
parameters._currentTransaction = depTran;
PopulateObjectThread(a);
// PopulateObjectThread code

CustomParameters a = stateInfo as CustomParameters;
DependentTransaction dependentTransaction;
dependentTransaction = a._currentTransaction as DependentTransaction;

Transaction oldTransaction = Transaction.Current;
try
{
      Transaction.Current = dependentTransaction;
      // DO WORK 
      dependentTransaction.Complete();
}

As you can see the original method being called "PopulateObjectThread" used to be a Thread body but that is no longer the case.

In order to share the transaction with the main thread a Dependant Transaction was created and assigned in the thread body. This was the exact action that was causing the error. Assigning the current transaction when COM+ Interop is enabled.

In our code it didn't make sense to do that anymore as we don't use threads so we just removed that bit of code. Other option would have been to move the code to a Thread and keep the code as it is, which also seem to work.

So if you find this error think that the reason is:
- You have a transaction with full interop enabled
- You are trying to change the current transaction in the thread that has initiated the full interop transaction

So you know what to do.

Hope this is of help to someone. With some luck next guy googling for this error will find something he can read, please leave a comment so I know of your luck!

No comments:

Post a Comment