Membership.GetUser () in TransactionScope throws TransactionPromotionException

The following code displays a message TransactionAbortedExceptionwith the message "Transaction canceled" and an internal TransactionPromotionExceptionmessage with "Failure while trying to promote the transaction":

    using ( TransactionScope transactionScope = new TransactionScope() )
    {
        try
        {
            using ( MyDataContext context = new MyDataContext() )
            {
                Guid accountID = new Guid( Request.QueryString[ "aid" ] );
                Account account = ( from a in context.Accounts where a.UniqueID.Equals( accountID ) select a ).SingleOrDefault();
                IQueryable < My_Data_Access_Layer.Login > loginList = from l in context.Logins where l.AccountID == account.AccountID select l;

                foreach ( My_Data_Access_Layer.Login login in loginList )
                {
                    MembershipUser membershipUser = Membership.GetUser( login.UniqueID );
                }

                [... lots of DeleteAllOnSubmit() calls]

                context.SubmitChanges();
                transactionScope.Complete();
            }   
        }

        catch ( Exception E )
        {
        [... reports the exception ...]
        }
    }

Error while calling Membership.GetUser().

My connection string:

      <add name="MyConnectionString" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=True"
   providerName="System.Data.SqlClient" />

Everything I read says it TransactionScopeshould just be magically applied to the challenges of Membership. User exists (otherwise I would expect a null return).

+3
source share
2 answers

TransactionScope . , , - ( ), TransactionAbortedException , , using.

TransactionScope try-catch catch ; , .

, TransactionScope.Complete , using, TransactionScope. , , , - , Complete .


Update:

, , (, ), , .

, TransactionScope GetUser. , DataContext, ; , TransactionScope , .

, , , MSDTC -, .

, , :

  • GetUser TransactionScope. " " , , .

  • GetUser , DataContext , , .

  • DTC , ( ).

, №1 ; , , , , , .

+6

; ( Complete()). ?

, DataContext TransactionScope , , , . ():

  • , TransactionScope DataContext
  • Complete

?

using ( TransactionScope transactionScope = new TransactionScope() )
using ( MyDataContext context = new MyDataContext() )
{
    /* ... */
    transactionScope.Complete();
}
+1

Source: https://habr.com/ru/post/1737916/


All Articles