I am trying to use TransactionScope
, but keep getting the exception below.
The app is running on a different machine than the database, if that matters. I am using SQL Server 2005.
Network access for Distributed Transaction Manager (MSDTC) has been disabled. Please enable DTC for network access in the security configuration for MSDTC using the Component Services Administrative tool.
using (TransactionScope tsTransScope = new TransactionScope()) { //Do stuff here tsTransScope.Complete(); }
Edit
I made some changes based on the feedback. Now I'm getting this error:
"Error HRESULT E_FAIL has been returned from a call to a COM component."
"Communication with the underlying transaction manager has failed."
Solution I think the accepted answer fixed the initial issue I was getting. The 2nd error seems to be specific to Entity Framework. I'll post another question for it.
Here are the properties on the client:
Client http://www.portnine.com/data/images/Misc/client.jpg
Here are the properties on the server:
Server http://www.portnine.com/data/images/Misc/server.jpg
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.
The term "ambient" transaction refers to a transaction that was started higher-up in the call stack. So that this is a per-thread concept. See Transaction.Current and TransactionScope.
You need to enable network DTC access as described in this Microsoft TechNet Article. This change may have to be made on both the database and application servers. Often times DTC is already turned on a database server so I'd look at the application server first.
Here is a screen shot of what we use except for the "Allow Remote Administration" option:
I have not run into the HRESULT E_Fail issue you are now having but this article on XP SP2 and transactions had this interesting suggestion:
Another configuration setting that you need to be aware (although I consider it to be an uncommon scenario) is RestrictRemoteClients registry key. If the value of this key is set to 2 (RPC_RESTRICT_REMOTE_CLIENT_HIGH) then MSDTC network transactions will not be able to work properly. MSDTC supports only RPC_RESTRICT_REMOTE_CLIENT_NONE (0) and RPC_RESTRICT_REMOTE_CLIENT_DEFAULT (1) values. See http://www.microsoft.com/technet/prodtechnol/winxppro/maintain/sp2netwk.mspx#XSLTsection128121120120 for more info on RestrictRemoteClients.
Finally, while not specific to your issue a very important thing to note about using the TransactionScope
class is that its default setting is to utilize a Transaction Isolation Level of Serializable. Serializable is the most restrictive of the isolation levels and frankly its surprising that it was chosen as the default. If you do not need this level of locking I would highly recommend setting the isolation level to a less restrictive option (ReadCommitted) when instantiating a TransactionScope
:
var scopeOptions = new TransactionOptions(); scopeOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted; scopeOptions.Timeout = TimeSpan.MaxValue; using (var scope = new TransactionScope(TransactionScopeOption.Required, scopeOptions)) { // your code here }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With