具有成员资格和角色的TransactionScope在同一块中调用(仅使用一个连接?)
|
我在同一事务范围内调用了Membership API和Roles API。我已经读到,打开多个连接会导致升级,需要启用分布式事务,所以我正在寻找一种打开一个连接并与之共享的方法:成员资格,角色,我自己的电话。
这是导致不必要的升级的工作代码:
public static void InsertUser(string userName, string email, string roleName, int contactId, string comment)
{
/*
* Exceptions thrown here are caught in the DetailView\'s 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();
}
}
谢谢
没有找到相关结果
已邀请:
2 个回复
倾坞髓
浮凰量