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)
{
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));
}
user.Comment = comment;
Membership.UpdateUser(user);
Roles.AddUserToRole(user.UserName, roleName);
Guid userId = (Guid)ExceptionUtils.ThrowIfDefaultValue(user.ProviderUserKey, "ProviderUserkey is null!");
insertIntoAspnet_Users_To_Contact(userId, contactId);
EmailUtils.SendWelcomeEmailFromAdmin(userName, email, password, passwordQuestion, passwordAnswer, roleName);
transactionScope.Complete();
}
}
thank
source
share