TransactionScope with Membership and Roles privileges in one block (a way to use only one connection?)

I have access to membership APIs and role APIs in the same transaction scope. I read that opening more than one connection causes an escalation that requires distributed transactions to be enabled, so I'm looking for a way to open one connection and share it with: membership, roles, my own calls.

Here's the working code that causes the unwanted escalation:

public static void InsertUser(string userName, string email, string roleName, int contactId, string comment)
      {
         /*
          * Exceptions thrown here are caught in the DetailView event handler and piped to the UI.
          */

         using(var transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew))
         {
            string password = Membership.GeneratePassword(Membership.MinRequiredPasswordLength, Membership.MinRequiredNonAlphanumericCharacters);
            const string passwordQuestion = "Should you change your password question?";
            const string passwordAnswer = "yes";
            MembershipCreateStatus status;
            MembershipUser user = Membership.CreateUser(userName, password, email, passwordQuestion, passwordAnswer, true, out status);

            if(user == null)
            {
               throw new Exception(GetErrorMessage(status));
            }

            // Flesh out new user
            user.Comment = comment;
            Membership.UpdateUser(user);

            // Give user a role
            Roles.AddUserToRole(user.UserName, roleName);

            // Create bridge table record
            Guid userId = (Guid)ExceptionUtils.ThrowIfDefaultValue(user.ProviderUserKey, "ProviderUserkey is null!");
            insertIntoAspnet_Users_To_Contact(userId, contactId);

            // Send welcome email
            EmailUtils.SendWelcomeEmailFromAdmin(userName, email, password, passwordQuestion, passwordAnswer, roleName);

            transactionScope.Complete();
         }
      }

thank

+3
source share
2 answers

SQL2008 , , MSDTC. , ​​ .

SQL-, , . , . SQL2005, .

+2

TransactionScopeOption.RequiresNew, , , , .

:

using(var transactionScope = new TransactionScope())
0

All Articles