Sara N
Sara N

Reputation: 1189

TransactionScope needs DTC enabled in C# MVC

We are using RDS (Amazon Relational Database Service) for our db . we have some sp s which called in a transactionScope. we ahve Customized ExecutionStrategy for our DBConfig like this

public class MpDbConfiguration : DbConfiguration
    {
        public MpDbConfiguration()
        {
            //SetExecutionStrategy(
            //    "System.Data.SqlClient", () => new MpExecutionStrategy(10, TimeSpan.FromMilliseconds(100)));

            SetExecutionStrategy("System.Data.SqlClient", () => SuspendExecutionStrategy
                ? (IDbExecutionStrategy)new DefaultExecutionStrategy()
                : new MpExecutionStrategy(10, TimeSpan.FromMilliseconds(100)));
        }
//.....
}

SuspendExecutionStrategy is set to True when we have user transaction (related article made me use this defaultStrategy : https://learn.microsoft.com/en-us/ef/ef6/fundamentals/connection-resiliency/retry-logic)

Issue : I have this issue when I run the transactions like this

   using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted }))
                {

                    if (entity != null && !string.IsNullOrEmpty(entity.EmailAddress))
                    {
                        ObjectFactory.GetInstance<IBankingService>().UnRegister(RequestContext.Current, entity);
                    }

                    Data.Configuration.MpDbConfiguration.SuspendExecutionStrategy = true;
                    Context.Current.Database.ExecuteSqlCommand("DeleteAccountByEmailAddress @usertodelete",
                                                               new SqlParameter("usertodelete", emailAddress));
                    scope.Complete();
                    Data.Configuration.MpDbConfiguration.SuspendExecutionStrategy = false;
                }
//....

This SP is a very big transaction but uses JUST one database . The error I get is to Enable DTC. my question is WHY I NEED DTC

The underlying provider failed on Open. 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.

Actually these terms are pretty new for me, but based on a search I found out DTC is just used when we have Distributed Transactions. In out case, we dont have it .

Upvotes: 0

Views: 1001

Answers (1)

Sara N
Sara N

Reputation: 1189

A solution might be to use the legacy type of transactions instead of transaction Scope. apparently a service there maybe use another context .I also added try catch to rollback the transaction in case of any exception.

Thanks @MarcGravell for the hints.

   using (var dbContextTransaction = Context.Current.Database.BeginTransaction())
                {
                    try
                    {
                     //--- the code run inside the transaction 
                        Context.Current.SaveChanges();
                        dbContextTransaction.Commit();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        dbContextTransaction.Rollback();
                       //....
                    }
                 }

Upvotes: 0

Related Questions