TransactionScope does not perform transactions

6

I'm trying to use TransactionScope in C #, I'm running tests with local bank, but every time I try to execute some method it returns me this exception:

Partner transaction manager has disabled your support for remote / network transactions. (Exception from HRESULT: 0x8004D025)

I've already enabled the Distributed Transaction Coordinator in Services and Local DTC in Component Services, to no avail.

Part of the code:

try
{
    using (var trans = new TransactionScope())
    {
        while (contadorInicial <= contadorFinal)
        {
            Objeto.Codigo = contadorInicial.ToString();
            _objController.Insert(Objeto);
            contadorInicial++;
        }

        trans.Complete();
        return true;
    }
}
    
asked by anonymous 19.05.2014 / 20:21

1 answer

5

There are several steps to enable support for distributed transactions:

  • Administrative Tools > Services
  • Enable the Distributed Transaction Coordinator if it is disabled;
  • Go back to Administrative Tools > Component Services;
  • Go to Component Services > Computers > My Computer > Right click > Properties;
  • On the MSDTC tab, click Configurações de Segurança ;
  • Apply the settings and restart your computer.
  • If you do not necessarily need to use TransactionScope , try the following alternatives:

    1. Using Entity Framework

    using (var tx = context.BeginTransaction())
    {
        while (contadorInicial <= contadorFinal)
        {
            Objeto.Codigo = contadorInicial.ToString();
            _objController.Insert(Objeto);
            contadorInicial++;
        }
    
        tx.Commit();
    }
    

    2. Using SqlConnection or some derivative

    // connection é uma SqlConnection inicializada.
    using (var tx = connection.BeginTransaction())
    {
        while (contadorInicial <= contadorFinal)
        {
            Objeto.Codigo = contadorInicial.ToString();
            _objController.Insert(Objeto);
            contadorInicial++;
        }
    
        tx.Commit();
    }
    

    Here's a note: I do not know how your DAO works. In any case, it would be legal for your insert method to accept a transaction or context as an optional parameter.

        
    19.05.2014 / 20:38