How to use TransactionScope in C#?
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
}
So TransactionScope is only used when dealing with database
From docs:
The
System.Transactions
infrastructure makes transactional
programming simple and efficient throughout the platform by supporting
transactions initiated in SQL Server, ADO.NET, MSMQ, and the
Microsoft Distributed Transaction Coordinator (MSDTC).
So, you can't use System.Transactions
with the file system.
There is Transactional NTFS component but Microsoft strongly recommends developers utilize alternative means to achieve your applications needs.
Also you can look at TransactionalFileMgr
Or as said @ken2k, you need to implement IEnlistmentNotification
and allow manual standard file operations to work with TransactionScope
. For instance, to enable rollback for a write operation on an existing file, it first creates a backup of the file that will be written, then writes to the backuped file, and finally replaces the initial file with the backuped/modified file if the transaction is committed, or deletes the backup file if the transaction is rollbacked
TransactionScope transaction = new TransactionScope() VS TransactionScope s = context.Connection.BeginTransaction()
While Database. BeginTransaction() is used only for database related operations transaction, System. Transactions. ... TransactionScope for mixing db operations and C# code together in a transaction.
please see Below Links.Hope they help you:
TransactionScope vs Transaction in LINQ to SQL
Database.BeginTransaction vs Transactions.TransactionScope
Using TransactionScope to perform select
So what a transaction scope would be useful for is if you're doing a lot of things with the database. So say you are modifying 3 tables. You modify table 1, table 2, but then when you try to modify table 3 it fails. You don't want the changes made in tables 1 and 2 to keep if the 3rd failed. This is where you wrap it in a transaction scope because if there is an error, all of those changes get rolled back( or rather don't take) and you don't have to worry about it.
You can read more here.
Wrapping a query in a transaction scope just to get data however... I don't see any benefit. You are correct, there is no data manipulation so there really doesn't have to be a transaction scope. What I assume is some of your coworkers just saw someone else use one and the decided it would be a good idea if they did too.
Another oddity is the fact that they're completing the transaction scope in a finally. If there was an error thrown, you probably wouldn't want to complete a transaction.
Is there a way to use TransactionScope with an existing connection?
In fact, there is one way.
connection.EnlistTransaction(Transaction.Current)
It works and it doesnt promote transaction to distributed if not necessary (contrary to what documentation says)
HTH
Related Topics
Why Does the Is Operator Return False When Given Null
Project a Can't Reference a Class in Project B
How to Maximize the Browser Window in Selenium Webdriver (Selenium 2) Using C#
Is There a Complete Iequatable Implementation Reference
How to Apply a General Rule for Remapping All Property Names When Serializing with JSON.Net
How to Capitalize First Letter of First Name and Last Name in C#
Cannot Resolve Scoped Service from Root Provider .Net Core 2
Importing Excel into a Datatable Quickly
What's Wrong with This Division
Entity Framework Queryable Async
Suppress Null Value Types from Being Emitted by Xmlserializer
.Net (3.5) Formats Times Using Dots Instead of Colons as Timeseparator for It-It Culture
Best Way to Check for Nullable Bool in a Condition Expression (If ...)
How to Parse Dates with a Suffix "Th", "St" or "Nd" on the Day of the Month