eerrzz
eerrzz

Reputation: 404

TransactionScope throws exception

Im sure this is so basic question but i can't found any solution on google neither here.

when im trying to use that code block it throws an exception that 'System.Activities.Statements.TransactionScope': type used in a using statement must be implicitly convertible to 'System.IDisposable'

using (TransactionScope trans = new TransactionScope()) { }

Simply, i want to use TransactionScope in Linq code.

Thanks, Regards

Upvotes: 7

Views: 3407

Answers (3)

Alex J
Alex J

Reputation: 10205

You are likely using the wrong TransactionScope.

The one you're looking for is System.Transactions.TransactionScope, which resides in the System.Transactions.dll assembly. Make sure it's referenced (it's not by default), and that you are "using System.Transactions".

Upvotes: 8

jason
jason

Reputation: 241769

I don't know what System.Activities.Statements.TransactionScope is but you want System.Transactions.TransactionScope. You'll also need a reference to System.Transactions.dll.

From the documentation:

public sealed class TransactionScope : IDisposable

Note that System.Activities.Statements.TransactionScope lives in System.Activities.Statements which is described as

The System.Activities.Statements namespace contains activities that can be used to create workflows. This namespace contains activities that are used for flow control, interacting with collections and variables, exceptions, compensation, transactions, and interacting with legacy workflows.

which is clearly not appropriate.

Upvotes: 7

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 121037

You are using the wrong TransactionScope class. The right one is in the System.Transactions namespace. Your error indicates that you are using another class from System.Activities.Statements.

Upvotes: 2

Related Questions